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.

source

ʰr̲adians : Num a => a -> Float

function (private) · line 11

Degrees as radians, a Float.

ʰr̲adians ← { (f̲loat ⍵) × (p̲i @) ÷ 180 }         ⍝ private
Used in: ˡw̲alk

Points of a walk

ˡw̲alk : (Num a, Num b) => a -> b -> Float

function · line 22

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
}
Used in: ˡp̲oints

ˡp̲oints : Num a => a -> Float

function · line 34

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 }

Building walks

ˡt̲urn : Num a => a -> a -> a

function · line 43

The walk w with t degrees more turned before its first step: how pieces of a curve are joined (60 ᵗt̲urn w).

      "t:" u̲se< "Turtle"
      60 ᵗt̲urn 0 120 120
60 120 120
ˡt̲urn ← { t w → (t + 1 t̲ake w) c̲at 1 d̲rop w }

ˡp̲olygon : Int -> Float

function · line 50

A regular polygon of n sides, as a walk: n turns of 360 / n degrees, so it closes where it began.

      "t:" u̲se< "Turtle"
      ᵗp̲olygon 4
90.0 90.0 90.0 90.0
ˡp̲olygon ← { n → n r̲eshape 360 ÷ n }