librarylib/Turtle.xtl
Turtle: turtle graphics as arrays (a standard library, built into xetal). Import it with an alias of your choice: "t:" u_se< "Turtle". A walk is a vector of turns in degrees, one before each step forward; the turtle starts at the origin facing east (0 degrees, counting anticlockwise). No turtle moves: the headings are a running sum of the turns, the steps are their cosines and sines, and the positions are running sums of the steps. Draw the points with []P_ATH. Names with l: are exported; names with h: are private to this file.
ʰr̲adians : Num a => a -> Float
Degrees as radians, a Float.
ʰr̲adians ← { (f̲loat ⍵) × (p̲i @) ÷ 180 } ⍝ private
Points of a walk
ˡw̲alk : (Num a, Num b) => a -> b -> Float
The points of a walk with steps of the given lengths, 2 rows (x over
y), starting at the origin: lengths ᵗw̲alk turns; a single length
serves every step. A walk of n turns gives n + 1 points.
"t:" u̲se< "Turtle"
f̲loor 0.5 + 2 ᵗw̲alk 0 90 90 0 2 2 0 0 0 2 2
ˡw̲alk ← { lengths turns → h ← ʰr̲adians '+ s̲\ turns xs ← 0 c̲at '+ s̲\ (f̲loat lengths) × c̲os h ys ← 0 c̲at '+ s̲\ (f̲loat lengths) × s̲in h (2 c̲at t̲ally xs) r̲eshape xs c̲at ys }
ˡp̲oints : Num a => a -> Float
The points of a walk of unit steps: ᵗp̲oints turns.
"t:" u̲se< "Turtle"
f̲loor 0.5 + ᵗp̲oints ᵗp̲olygon 4 0 0 -1 -1 0 0 1 1 0 0
ˡp̲oints ← { turns → 1 ˡw̲alk turns }