diff --git a/dragon.ld b/dragon.ld new file mode 100644 index 0000000..4715021 --- /dev/null +++ b/dragon.ld @@ -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) diff --git a/koch.ld b/koch.ld index 81294e2..64dfe91 100644 --- a/koch.ld +++ b/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 angle = 0.25 diff --git a/sierp.ld b/sierp.ld index a6053fe..2a58f8c 100644 --- a/sierp.ld +++ b/sierp.ld @@ -3,10 +3,11 @@ & The Sierpinski triangle drawn using an L-system & variables : F G -& constants : + − -& start : F−G−G -& rules : (F → F−G+F+G−F), (G → GG) +& constants : + - +& start : F-G-G +& rules : (F -> F-G+F+G-F), (G -> GG) & 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 let length = 10 diff --git a/spiralgon.ld b/spiralgon.ld deleted file mode 100644 index 02d44d5..0000000 --- a/spiralgon.ld +++ /dev/null @@ -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)