2024-06-11 21:37:04 +00:00
|
|
|
|
& Example Ludus implementation of the Sierpinski triangle.
|
|
|
|
|
& https://en.m.wikipedia.org/wiki/L-system#Example_5:_Sierpinski_triangle
|
|
|
|
|
|
|
|
|
|
& The Sierpinski triangle drawn using an L-system
|
|
|
|
|
& variables : F G
|
2024-06-14 17:52:54 +00:00
|
|
|
|
& constants : + -
|
|
|
|
|
& start : F-G-G
|
|
|
|
|
& rules : (F -> F-G+F+G-F), (G -> GG)
|
2024-06-11 21:37:04 +00:00
|
|
|
|
& angle : 120°
|
2024-06-14 17:52:54 +00:00
|
|
|
|
& F and G both mean "draw forward", + means "turn left by angle", and − means "turn right by angle".
|
2024-06-11 21:37:04 +00:00
|
|
|
|
|
|
|
|
|
& set a length for each segment
|
|
|
|
|
let length = 10
|
|
|
|
|
|
|
|
|
|
& set an angle for all turns, default to 1/3 for triangles
|
|
|
|
|
let angle = inv(1)
|
|
|
|
|
|
|
|
|
|
& define 'G' function from the grammaer
|
|
|
|
|
fn g! {
|
|
|
|
|
(0) -> fd!(length)
|
|
|
|
|
(n) -> {
|
|
|
|
|
g!(dec(n))
|
|
|
|
|
g!(dec(n))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
& define 'F' function from the grammaer
|
|
|
|
|
fn f! {
|
|
|
|
|
(0) -> fd!(length)
|
|
|
|
|
(n) -> {
|
|
|
|
|
f!(dec(n))
|
|
|
|
|
rt!(angle)
|
|
|
|
|
g!(dec(n))
|
|
|
|
|
lt!(angle)
|
|
|
|
|
f!(dec(n))
|
|
|
|
|
lt!(angle)
|
|
|
|
|
g!(dec(n))
|
|
|
|
|
rt!(angle)
|
|
|
|
|
f!(dec(n))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
& this function defines the starting pattern.
|
|
|
|
|
& each of the f! and g! calls expand based on the grammar at the top of this example
|
|
|
|
|
fn sierp! (times) -> {
|
|
|
|
|
f!(times)
|
|
|
|
|
rt!(angle)
|
|
|
|
|
g!(times)
|
|
|
|
|
rt!(angle)
|
|
|
|
|
g!(times)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
& call the base function with a number of iterations
|
|
|
|
|
& remember, this is exponential! high numbers can freeze your browser.
|
|
|
|
|
sierp!(7)
|