How to resolve the algorithm Sierpinski triangle/Graphical step by step in the Rust programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sierpinski triangle/Graphical step by step in the Rust programming language

Table of Contents

Problem Statement

Produce a graphical representation of a Sierpinski triangle of order N in any orientation.
An example of Sierpinski's triangle (order = 8) looks like this:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sierpinski triangle/Graphical step by step in the Rust programming language

Source code in the rust programming language

// [dependencies]
// svg = "0.8.0"

const SQRT3_2: f64 = 0.86602540378444;

fn sierpinski_triangle(
    mut document: svg::Document,
    mut x: f64,
    mut y: f64,
    mut side: f64,
    order: usize,
) -> svg::Document {
    use svg::node::element::Polygon;

    if order == 1 {
        let mut points = Vec::new();
        points.push((x, y));
        y += side * SQRT3_2;
        x -= side * 0.5;
        points.push((x, y));
        x += side;
        points.push((x, y));
        let polygon = Polygon::new()
            .set("fill", "black")
            .set("stroke", "none")
            .set("points", points);
        document = document.add(polygon);
    } else {
        side *= 0.5;
        document = sierpinski_triangle(document, x, y, side, order - 1);
        y += side * SQRT3_2;
        x -= side * 0.5;
        document = sierpinski_triangle(document, x, y, side, order - 1);
        x += side;
        document = sierpinski_triangle(document, x, y, side, order - 1);
    }
    document
}

fn write_sierpinski_triangle(file: &str, size: usize, order: usize) -> std::io::Result<()> {
    use svg::node::element::Rectangle;

    let margin = 20.0;
    let side = (size as f64) - 2.0 * margin;
    let y = 0.5 * ((size as f64) - SQRT3_2 * side);
    let x = margin + side * 0.5;

    let rect = Rectangle::new()
        .set("width", "100%")
        .set("height", "100%")
        .set("fill", "white");

    let mut document = svg::Document::new()
        .set("width", size)
        .set("height", size)
        .add(rect);

    document = sierpinski_triangle(document, x, y, side, order);
    svg::save(file, &document)
}

fn main() {
    write_sierpinski_triangle("sierpinski_triangle.svg", 600, 8).unwrap();
}


  

You may also check:How to resolve the algorithm Keyboard input/Obtain a Y or N response step by step in the BASIC programming language
You may also check:How to resolve the algorithm Object serialization step by step in the Insitux programming language
You may also check:How to resolve the algorithm Bitmap/Bézier curves/Quadratic step by step in the D programming language
You may also check:How to resolve the algorithm Rhonda numbers step by step in the C++ programming language
You may also check:How to resolve the algorithm Atomic updates step by step in the Python programming language