programdemos/rosetta/cube.xtl

source · imports g: lib/Geometry3D.xtl; v: lib/Svg.xtl

size : Float

value · line 13
size ← 300.0                 ⍝ the picture is size by size

eye : Float

value · line 14
eye ← 6.0                    ⍝ the viewer's distance along z
Used in: ᵘs̲creen

zoom : Float

value · line 15
zoom ← 70.0                  ⍝ pixels per unit
Used in: ᵘs̲creen

light : Float

value · line 16
light ← 0.3 0.5 0.8          ⍝ from the viewer's side, upper right
Used in: ᵘl̲it

cube : Float

value · line 18
cube ← ᵍc̲ube 2
Used in: ᵘs̲cene

faces : Int

value · line 19
faces ← ᵍc̲ubeFaces @
Used in: ᵘs̲cene

ᵘs̲creen : Float -> Float

function · line 22
ᵘs̲creen ← { f →
  p ← eye ᵍp̲roject f
  xs ← (size ÷ 2) + zoom × 1 s̲elect p
  ys ← (size ÷ 2) − zoom × 2 s̲elect p
  (2 c̲at t̲ally xs) r̲eshape xs c̲at ys
}
Used in: ᵘp̲olygon

ᵘn̲ormal : Num a => a -> a

function · line 30
ᵘn̲ormal ← { f →
  u ← (2 s̲elect₂ f) − 1 s̲elect₂ f
  w ← (4 s̲elect₂ f) − 1 s̲elect₂ f
  ((1 o̲- u) × 2 o̲- w) − (2 o̲- u) × 1 o̲- w
}
Used in: ᵘl̲it

ᵘl̲it : Float -> Float

function · line 39
ᵘl̲it ← { f →
  n ← ᵘn̲ormal f
  center ← ('+ r̲/ o̲\ f) ÷ 4.0
  outward ← n × (2.0 × f̲loat 0 < '+ r̲/ n × center) − 1.0
  0 m̲ax ('+ r̲/ outward × light) ÷ ('+ r̲/ n × n) ^ 0.5
}
Used in: ᵘp̲olygon

ᵘp̲olygon : Truthy a => a -> Float -> Char

function · line 47
ᵘp̲olygon ← { shaded f →
  fill ← ᵛf̲ill "shaded" i̲f< "v:r_gb 3 r_eshape 40 + 200 * u:l_it f; \"none\""
  (fill c̲at "#222" ᵛs̲troke 1.5) ᵛp̲olygon f̲loor 0.5 + ᵘs̲creen f
}
i̲f< expands to
({ @ → (shaded) ? ᵛr̲gb 3 r̲eshape 40 + 200 × ᵘl̲it f◆ "none" } @)
Used in: ᵘs̲cene

ᵘs̲cene : Truthy a => a -> Float -> Char

function · line 54
ᵘs̲cene ← { shaded a →
  turned ← ((ᵍr̲otY 1.3 × a) ᵍt̲urn ᵍr̲otX a) ᵍt̲urn cube
  solid ← faces ᵍs̲olid turned
  polygons ← '{ i → shaded ᵘp̲olygon i s̲elect solid } m̲ap ᵍo̲rder solid
  (size c̲at size) ᵛp̲icture d̲isclose '{ x y → e̲nclose (d̲isclose x) c̲at d̲isclose y } r̲/ polygons
}
Used in: ᵘf̲rame

ᵘf̲rame : Truthy a => a -> Float -> Unit

function · line 61
ᵘf̲rame ← { shaded a → ⎕S̲HOW shaded ᵘs̲cene a◆ ⎕D̲L 0.04◆ @ }
Used in: shown

angles : Float

value · line 63
angles ← 30.0 × f̲loat o̲ffsets 12
Used in: shown

shown : Unit

value · line 64
shown ← '{ a → 0 ᵘf̲rame a } e̲ach angles

shown : Unit

value · line 65
shown ← '{ a → 1 ᵘf̲rame a } e̲ach angles