How to resolve the algorithm Pythagoras tree step by step in the Rust programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Pythagoras tree step by step in the Rust programming language

Table of Contents

Problem Statement

The Pythagoras tree is a fractal tree constructed from squares. It is named after Pythagoras because each triple of touching squares encloses a right triangle, in a configuration traditionally used to represent the Pythagorean theorem.

Construct a Pythagoras tree of order 7 using only vectors (no rotation or trigonometric functions).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Pythagoras tree step by step in the Rust programming language

Source code in the rust programming language

use svg::node::element::{Group, Polygon};

fn main() {
    let mut base: Vec<[(f64, f64); 2]> = vec![[(-200., 0.), (200., 0.)]];
    let doc = (0..12_u8).fold(svg::Document::new().set("stroke", "white"), |doc_a, lvl| {
        let rg = |step| lvl.wrapping_mul(step).wrapping_add(80 - step * 2);
        let g = Group::new().set("fill", format!("#{:02X}{:02X}18", rg(20), rg(30))); // level color
        doc_a.add(base.split_off(0).into_iter().fold(g, |ga, [a, b]| {
            let v = (b.0 - a.0, b.1 - a.1);
            let [c, d, w] = [a, b, v].map(|p| (p.0 + v.1, p.1 - v.0));
            let e = (c.0 + w.0 / 2., c.1 + w.1 / 2.);
            base.extend([[c, e], [e, d]]);
            ga.add(Polygon::new().set("points", vec![a, c, e, d, c, d, b]))
        }))
    });
    let (x, y) = (base.iter()).fold((0., 0.), |(xa, ya), [p, _]| (p.0.min(xa), p.1.min(ya)));
    svg::save("Pythagor_tree.svg", &doc.set("viewBox", (x, y, -x - x, -y))).unwrap();
}


  

You may also check:How to resolve the algorithm Guess the number step by step in the Sidef programming language
You may also check:How to resolve the algorithm Loops/While step by step in the Trith programming language
You may also check:How to resolve the algorithm Entropy/Narcissist step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Pythagorean quadruples step by step in the Julia programming language
You may also check:How to resolve the algorithm Hailstone sequence step by step in the Lasso programming language