adding more l-system examples

This commit is contained in:
Matt Nish-Lapidus 2024-06-14 13:52:54 -04:00
parent 58030af132
commit 82145a8f08
4 changed files with 61 additions and 50 deletions

47
dragon.ld Normal file
View 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
View File

@ -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+FFF+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

View File

@ -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 : FGG & start : F-G-G
& rules : (F → FG+F+GF), (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

View File

@ -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)