sourcelib/Turtle.xtl

1⍝ Turtle: turtle graphics as arrays (a standard library, built into 2⍝ xetal). Import it with an alias of your choice: "t:" u_se< "Turtle". 3⍝ A walk is a vector of turns in degrees, one before each step forward; 4⍝ the turtle starts at the origin facing east (0 degrees, counting 5⍝ anticlockwise). No turtle moves: the headings are a running sum of 6⍝ the turns, the steps are their cosines and sines, and the positions 7⍝ are running sums of the steps. Draw the points with []P_ATH. 8 9r̲adians ← { (f̲loat ⍵) × (p̲i @) ÷ 180 } ⍝ private 10 11⍝ The points of a walk with steps of the given lengths, 2 rows (x over 12⍝ y), starting at the origin: lengths ˡw̲alk turns; a single length 13⍝ serves every step. 14ˡw̲alk ← { lengths turns → 15 h ← r̲adians '+ s̲\ turns 16 xs ← 0 c̲at '+ s̲\ (f̲loat lengths) × c̲os h 17 ys ← 0 c̲at '+ s̲\ (f̲loat lengths) × s̲in h 18 (2 c̲at t̲ally xs) r̲eshape xs c̲at ys 19} 20 21⍝ The points of a walk of unit steps. 22ˡp̲oints ← { turns → 1 ˡw̲alk turns } 23 24⍝ The walk with t degrees more turned before its first step: how pieces 25⍝ of a curve are joined (60 ˡt̲urn w). 26ˡt̲urn ← { t w → (t + 1 t̲ake w) c̲at 1 d̲rop w } 27 28⍝ A regular polygon of n sides, as a walk. 29ˡp̲olygon ← { n → n r̲eshape 360 ÷ n }