Skip to content

Instantly share code, notes, and snippets.

@jdh30
Created November 21, 2022 15:25
Show Gist options
  • Save jdh30/b1c9822aaeb110e4806cbcc958c53349 to your computer and use it in GitHub Desktop.
Save jdh30/b1c9822aaeb110e4806cbcc958c53349 to your computer and use it in GitHub Desktop.
Pythagoras' Tree
let branches m =
{ { Vec2.rotate(π/2 - asin(4/5));
Vec2.scale(4/5, 4/5);
Vec2.translate(0, -1) };
{ Vec2.translate(1, -1);
Vec2.rotate(-π/2 + asin(3/5));
Vec2.scale(3/5, 3/5);
Vec2.translate(1/3, 0) } }
@ Array.map [m2 → Array.concat{m; m2}]
let square =
{0,0; 1,0; 1,1; 0,1}
@ Array.map Line
@ Closed
let shape color m =
let style = Style(Fill color, NoStroke) in
Shape(style, Path(Contour.transform m square))
let trunks color ms = Array.map (shape color) ms
let burlyWood = Hex "deb887"
let rec tree n ms =
if n=0 then {trunks Green ms} else
let ms2 = Array.collect branches ms in
Array.concat{{trunks burlyWood ms}; tree (n-1) ms2}
let () =
tree 8 {{Vec2.translate (300, 300); Vec2.scale (100, 100)}}
@ Array.concat
@ Html.ofSvg (600, 600)
@ yield
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment