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)
k : Int
k ← w ˢᶜc̲amera! 0.6 0.45 7.5 0.0
scramble : Int
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
frames : Int
frames for a quarter turn, still after it, and still at the scrambled cube before solving
frames ← 20
ᵘp̲aint : (Any a, Num b) => (Int, Int, Int) -> a -> b
(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 }
ᵘa̲fter : (Int, Int) -> a -> a
(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 }
ᵘa̲nnounce : (Num a, Num b) => a -> Int -> b
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 }
ᵘs̲ay : (Num a, Truthy a, Num b) => a -> Char -> b
ᵘs̲ay ← { yes m → 0 = yes ? 0 said ← p̲rint! m 0 }
ᵘs̲how : Any a => (Int, Int, Int, a) -> (Int, Int, Int, a)
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) }
ᵘf̲rame : Num a => (Int, Int, Int, a) -> (Int, Int, Int, a)
ᵘ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 }