2024-06-24 22:59:39 +00:00
|
|
|
& Example 2: fractal (binary) tree
|
|
|
|
& from https://en.wikipedia.org/wiki/L-system
|
|
|
|
box states = []
|
|
|
|
|
|
|
|
fn push! () -> {
|
2024-11-03 22:32:15 +00:00
|
|
|
update! (states, append (_, unbox (turtle_state)))
|
2024-06-24 22:59:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn pop! () -> {
|
|
|
|
let state = do states > unbox > last
|
|
|
|
update! (states, butlast)
|
|
|
|
penup! ()
|
2024-11-03 22:32:15 +00:00
|
|
|
loadstate! (state)
|
2024-06-24 22:59:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn branch! () -> {
|
2024-11-03 22:32:15 +00:00
|
|
|
pc! (colors :green)
|
2024-06-24 22:59:39 +00:00
|
|
|
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! ()
|
|
|
|
|