adding scott's examples from the computer class repo

This commit is contained in:
Matt Nish-Lapidus 2024-06-11 17:55:57 -04:00
parent 50eb33a7f2
commit 51ba3a8bd9
4 changed files with 144 additions and 4 deletions

64
10print.ld Normal file
View File

@ -0,0 +1,64 @@
penup! ()
goto! (150, 150) & should be (-150, -150)
right! (0.25)
let root_2 = 1.414213562
let cell_size = 15
let line_length = mult (root_2, cell_size)
fn upstroke! () -> {
print! (:up)
penup! ()
forward! (cell_size)
left! (0.125)
pendown! ()
back! (line_length)
penup! ()
forward! (line_length)
right! (0.125)
}
fn downstroke! () -> {
print! (:down)
pendown! ()
right! (0.125)
forward! (line_length)
left! (0.375)
penup! ()
forward! (cell_size)
right! (0.25)
}
fn up_or_down! () -> {
let rand = random ()
if lt? (rand, 0.5)
then upstroke! ()
else downstroke! ()
}
fn newline! () -> {
penup! ()
back! (mult (grid_size, cell_size))
right! (0.25)
forward! (cell_size)
left! (0.25)
}
let grid_size = 20
fn randcolor! () -> pencolor! ((random (0, 255), random (0, 255), random (0, 255)))
repeat grid_size {
randcolor! ()
up_or_down! ()
}
& repeat grid_size {
& newline! ()
& }

44
gons_recur.ld Normal file
View File

@ -0,0 +1,44 @@
fn ngon! (sides, size) -> {
repeat sides {
forward! (size)
right! (div (1, sides))
}
:tada!
}
fn randcolor () -> (random (0, 255), random (0, 255), random (0, 255))
fn pen_randcolor! () -> pencolor! (randcolor ())
fn spiral_gon! (iter) -> {
loop (30, 0, colors :lime) with (size, times, color) -> {
let randcolor = (random(0, 255), random(0, 255), random (0, 255))
let newcolor = if eq? (color, colors :lime)
then colors :fuchsia
else colors :lime
if lt? (times, iter)
then {
pen_randcolor! ()
ngon! (5, size)
right! (inv (iter))
recur (add (size, 1), inc (times), newcolor)
}
else :ok
}
}
& spiral_gon! (100)
fn spiral_gonr! {
(iter) -> spiral_gonr! (iter, iter, 30)
(_, 0, _) -> :ok
(iter, times, size) -> {
pen_randcolor! ()
ngon! (4, size)
right! (inv (iter))
spiral_gonr! (iter, dec (times), inc (size))
}
}
goto! (37, 37)
spiral_gonr! (100)
print! (turtle_state())

View File

@ -1,18 +1,19 @@
& koch curve & koch curve
let length = 10 let length = 10
let angle = 0.25
fn koch! { fn koch! {
(0) -> fd! (length) (0) -> fd! (length)
(n) -> { (n) -> {
koch! (dec (n)) koch! (dec (n))
lt! (0.25) lt! (angle)
koch! (dec (n)) koch! (dec (n))
rt! (0.25) rt! (angle)
koch! (dec (n)) koch! (dec (n))
rt! (0.25) rt! (angle)
koch! (dec (n)) koch! (dec (n))
lt! (0.25) lt! (angle)
koch! (dec (n)) koch! (dec (n))
} }
} }

31
koch_tri.ld Normal file
View File

@ -0,0 +1,31 @@
& koch triangles!
let angle = inv (3)
let half = div (_, 2)
fn koch! {
(0, len) -> fd! (len)
(n, len) -> {
let newlen = div (len, 3)
let next = dec (n)
koch! (next, newlen)
left! (half (angle))
koch! (next, newlen)
right! (angle)
koch! (next, newlen)
left! (half (angle))
koch! (next, newlen)
}
}
& just give ourselves some runway
penup! ()
left! (0.25)
forward! (350)
right! (0.5)
pendown! ()
& make the triangles!
koch! (6, 800)