programextensions/scene/demos/voxels-rubik-turn.xtl

Voxels, 9: a quarter turn, seen. One layer of the Rubik's cube -- 9 cubies, a "plane" -- turns a quarter revolution while the rest stays still: its cubies' bodies and stickers rotated a little more each frame (a rotation matrix, one inner product), then the cube's colors permuted once it is done. A scramble: twelve quarter turns, each on a different axis from the one before (the faces in the order U R F D L B, twice), the directions + + - - + - and then - + + + - +; a pause; then the same turns undone in reverse, back to solved. Run: just demo scene voxels-rubik-turn (drag to turn it; q quit)

source · imports rb: extensions/scene/demos/Rubik.xtl; sc: extensions/scene/lib/Scene.xtl

w : Int

value · line 14
w ← "Voxels 9: a quarter turn -- X_eTaL" ˢᶜo̲pen! 560 560

k : Int

value · line 15
k ← w ˢᶜc̲amera! 0.6 0.45 7.5 0.0

scramble : Int

value · line 20

the scramble, in the order of rb:names: U R' ... -- each turn on another axis than the one before -- then the way back: the same turns undone, last first

scramble ← 1 5 10 4 7 12 2 5 9 3 8 11

turns : Int

value · line 21
turns ← scramble c̲at ʳᵇu̲ndo r̲ev scramble

frames : Int

value · line 24

frames for a quarter turn, still after it, and still at the scrambled cube before solving

frames ← 20

rest : Int

value · line 25
rest ← 8
Used in: ᵘs̲how

pause : Int

value · line 26
pause ← 50
Used in: ᵘs̲how

names : Box Char

value · line 28
names ← (n̲ot ʳᵇnames = f̲irst " ") p̲artition ʳᵇnames
Used in: ᵘa̲nnounce

ᵘp̲aint : (Any a, Num b) => (Int, Int, Int) -> a -> b

function · line 32

(colors, turn, frame) u:p_aint 0: the cube drawn at this frame of the turn -- turning, or (at its end) with the turn made

ᵘp̲aint ← { (c, i, f) z →
  f < frames ? (c, i, (f̲loat f + 1) ÷ f̲loat frames) ʳᵇd̲raw w
  f = frames ? (i ʳᵇt̲urn c, 0, 0.0) ʳᵇd̲raw w
  0
}
Used in: ᵘs̲how

ᵘa̲fter : (Int, Int) -> a -> a

function · line 40

(turn, frame) u:a_fter c: the colors after this frame: turned once, at the turn's end

ᵘa̲fter ← { (i, f) c →
  f = frames ? i ʳᵇt̲urn c
  c
}
Used in: ᵘs̲how

ᵘa̲nnounce : (Num a, Num b) => a -> Int -> b

function · line 46

the turn about to be shown, printed (and the start of each half)

ᵘa̲nnounce ← { yes j →
  0 = yes ? 0
  k ← 1 + (j − 1) m̲od t̲ally turns
  i ← k s̲elect turns
  said ← (k = 1) ᵘs̲ay "scrambling"
  said ← (k = 1 + t̲ally scramble) ᵘs̲ay "solving"
  said ← p̲rint! "turn " c̲at d̲isclose i s̲elect names
  0
}
Used in: ᵘs̲how

ᵘs̲ay : (Num a, Truthy a, Num b) => a -> Char -> b

function · line 55
ᵘs̲ay ← { yes m →
  0 = yes ? 0
  said ← p̲rint! m
  0
}
Used in: ᵘa̲nnounce

ᵘs̲how : Any a => (Int, Int, Int, a) -> (Int, Int, Int, a)

function · line 63

one frame of the show: the state is (colors, which turn, its frame, running)

ᵘs̲how ← { (c, j, f, on) →
  i ← (1 + (j − 1) m̲od t̲ally turns) s̲elect turns
  said ← (f = 0) ᵘa̲nnounce j
  drawn ← (c, i, f) ᵘp̲aint 0
  c ← (i, f) ᵘa̲fter c
  wait ← rest + pause × (0 = (j m̲od t̲ally scramble))
  f < frames + wait ? (c, j, f + 1, on)
  (c, j + 1, 0, on)
}
Used in: ᵘf̲rame

ᵘf̲rame : Num a => (Int, Int, Int, a) -> (Int, Int, Int, a)

function · line 73
ᵘf̲rame ← { st →
  (c, j, f, on) ← st
  0 = on ? st
  v ← ˢᶜn̲ext! w
  ((v m̲atch "close") + v m̲atch "key q") > 0 ? ᵘq̲uit st
  ᵘs̲how st
}
Used in: last

ᵘq̲uit : Num a => (Int, Int, Int, a) -> (Int, Int, Int, a)

function · line 80
ᵘq̲uit ← { (c, j, f, on) →
  z ← ˢᶜc̲lose! w
  (c, j, f, 0)
}
Used in: ᵘf̲rame

shown : Int

value · line 84
shown ← (ʳᵇsolved, 0, 0.0) ʳᵇd̲raw w

last : (Int, Int, Int, Int)

value · line 85
last ← 100000 'ᵘf̲rame p̲ower (ʳᵇsolved, 1, 0, 1)