adding more l-system examples
This commit is contained in:
parent
58030af132
commit
82145a8f08
47
dragon.ld
Normal file
47
dragon.ld
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
& Example Ludus implementation of the Dragon curve.
|
||||||
|
& https://en.m.wikipedia.org/wiki/L-system#Example_6:_dragon_curve
|
||||||
|
|
||||||
|
& variables : F G
|
||||||
|
& constants : + -
|
||||||
|
& start : F
|
||||||
|
& rules : (F -> F+G), (G -> F-G)
|
||||||
|
& angle : 90°
|
||||||
|
& F and G both mean "draw forward", + means "turn left by angle", and − means "turn right by angle"
|
||||||
|
|
||||||
|
& set a length for each segment
|
||||||
|
let length = 10
|
||||||
|
|
||||||
|
& set an angle for all turns, default to 1/3 for triangles
|
||||||
|
let angle = 0.25
|
||||||
|
|
||||||
|
fn f!
|
||||||
|
|
||||||
|
& define 'G' function from the grammaer
|
||||||
|
fn g! {
|
||||||
|
(0) -> fd!(length)
|
||||||
|
(n) -> {
|
||||||
|
f!(dec(n))
|
||||||
|
rt!(angle)
|
||||||
|
g!(dec(n))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
& define 'F' function from the grammaer
|
||||||
|
fn f! {
|
||||||
|
(0) -> fd!(length)
|
||||||
|
(n) -> {
|
||||||
|
f!(dec(n))
|
||||||
|
lt!(angle)
|
||||||
|
g!(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 dragon! (times) -> {
|
||||||
|
f!(times)
|
||||||
|
}
|
||||||
|
|
||||||
|
& call the base function with a number of iterations
|
||||||
|
& remember, this is exponential! high numbers can freeze your browser.
|
||||||
|
dragon!(10)
|
11
koch.ld
11
koch.ld
|
@ -1,4 +1,13 @@
|
||||||
& koch curve
|
& Example Ludus implementation of the Dragon curve.
|
||||||
|
& https://en.m.wikipedia.org/wiki/L-system#Example_4:_Koch_curve
|
||||||
|
|
||||||
|
& A variant of the Koch curve which uses only right angles.
|
||||||
|
& variables : F
|
||||||
|
& constants : + −
|
||||||
|
& start : F
|
||||||
|
& rules : (F -> F+F−F−F+F)
|
||||||
|
& Here, F means "draw forward", + means "turn left 90°", and − means "turn right 90°"
|
||||||
|
|
||||||
|
|
||||||
let length = 10
|
let length = 10
|
||||||
let angle = 0.25
|
let angle = 0.25
|
||||||
|
|
7
sierp.ld
7
sierp.ld
|
@ -3,10 +3,11 @@
|
||||||
|
|
||||||
& The Sierpinski triangle drawn using an L-system
|
& The Sierpinski triangle drawn using an L-system
|
||||||
& variables : F G
|
& variables : F G
|
||||||
& constants : + −
|
& constants : + -
|
||||||
& start : F−G−G
|
& start : F-G-G
|
||||||
& rules : (F → F−G+F+G−F), (G → GG)
|
& rules : (F -> F-G+F+G-F), (G -> GG)
|
||||||
& angle : 120°
|
& angle : 120°
|
||||||
|
& F and G both mean "draw forward", + means "turn left by angle", and − means "turn right by angle".
|
||||||
|
|
||||||
& set a length for each segment
|
& set a length for each segment
|
||||||
let length = 10
|
let length = 10
|
||||||
|
|
46
spiralgon.ld
46
spiralgon.ld
|
@ -1,46 +0,0 @@
|
||||||
let gons = 10
|
|
||||||
let sides = 4
|
|
||||||
let size = 50
|
|
||||||
|
|
||||||
fn randcolor () -> (random(0,255), random(0,255), random(0,255))
|
|
||||||
|
|
||||||
fn ngon! (sides, size) -> {
|
|
||||||
repeat sides {
|
|
||||||
fd! (size)
|
|
||||||
rt! (inv(sides))
|
|
||||||
}
|
|
||||||
:gon!
|
|
||||||
}
|
|
||||||
|
|
||||||
fn spiralgon_recursive! (num, sides, size) -> {
|
|
||||||
pencolor!(randcolor())
|
|
||||||
ngon! (sides, size)
|
|
||||||
|
|
||||||
if gt? (num, 0) then {
|
|
||||||
rt!(inv(gons))
|
|
||||||
spiralgonr!(dec (num), sides, add(10,size))
|
|
||||||
} else {
|
|
||||||
:tired!
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
& Non recursive version using loop for reference/example
|
|
||||||
& fn spiralgon! (num, sides) -> {
|
|
||||||
& loop (15, 0) with (size, times) -> {
|
|
||||||
& if lt? (times, num) then {
|
|
||||||
& pencolor!(randcolor())
|
|
||||||
& ngon! (sides, size)
|
|
||||||
& rt! (0.2)
|
|
||||||
& if gt? (times, div (num, 2)) then {
|
|
||||||
& fd! (20)
|
|
||||||
& } else {
|
|
||||||
& back! (200)
|
|
||||||
& }
|
|
||||||
& recur (add (size, 1), inc (times))
|
|
||||||
& } else :dizzy
|
|
||||||
& }
|
|
||||||
& }
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
spiralgon_recursive!(gons, sides, size)
|
|
Loading…
Reference in New Issue
Block a user