From 6c8c667042b4610a2556a42370603afabb8f8c37 Mon Sep 17 00:00:00 2001 From: Scott Richmond Date: Mon, 24 Jun 2024 18:59:39 -0400 Subject: [PATCH] add binary tree l-system from wikipedia --- binary_tree.ld | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 binary_tree.ld diff --git a/binary_tree.ld b/binary_tree.ld new file mode 100644 index 0000000..d8e6ad7 --- /dev/null +++ b/binary_tree.ld @@ -0,0 +1,48 @@ +& Example 2: fractal (binary) tree +& from https://en.wikipedia.org/wiki/L-system +box states = [] + +fn push! () -> { + update! (states, append (_, turtle_state ())) +} + +fn pop! () -> { + let state = do states > unbox > last + update! (states, butlast) + penup! () + load_turtle_state! (state) +} + +fn branch! () -> { + pc! (colors :olive) + pw! (4) + fd! (20) +} + +fn one! { + (0) -> { + branch! () + } + (n) -> { + one! (dec (n)) + one! (dec (n)) + } +} + +fn zero! { + (0) -> branch! () + (n) -> { + one! (dec (n)) + push! () + lt! (inv (10)) + zero! (dec (n)) + pop! () + rt! (inv (10)) + zero! (dec (n)) + } +} + +zero! (7) + +hideturtle! () +