sourceextensions/scene/demos/voxels-rubik-turn.xtl
1⍝# Voxels, 9: a quarter turn, seen. One layer of the Rubik's cube -- 9
2⍝# cubies, a "plane" -- turns a quarter revolution while the rest stays
3⍝# still: its cubies' bodies and stickers rotated a little more each
4⍝# frame (a rotation matrix, one inner product), then the cube's colors
5⍝# permuted once it is done. A scramble: twelve quarter turns, each on
6⍝# a different axis from the one before (the faces in the order U R F
7⍝# D L B, twice), the directions + + - - + - and then - + + + - +; a
8⍝# pause; then the same turns undone in reverse, back to solved.
9⍝# Run: just demo scene voxels-rubik-turn (drag to turn it; q quit)
10
11ʳᵇ⁼u̲se< "Rubik"
12ˢᶜ⁼u̲se< "Scene"
13
14w ← "Voxels 9: a quarter turn -- X_eTaL" ˢᶜo̲pen! 560 560
15k ← w ˢᶜc̲amera! 0.6 0.45 7.5 0.0
16
17⍝# the scramble, in the order of rb:names: U R' ... -- each turn on
18⍝# another axis than the one before -- then the way back: the same
19⍝# turns undone, last first
20scramble ← 1 5 10 4 7 12 2 5 9 3 8 11
21turns ← scramble c̲at ʳᵇu̲ndo r̲ev scramble
22⍝# frames for a quarter turn, still after it, and still at the
23⍝# scrambled cube before solving
24frames ← 20
25rest ← 8
26pause ← 50
27
28names ← (n̲ot ʳᵇnames = f̲irst " ") p̲artition ʳᵇnames
29
30⍝# (colors, turn, frame) u:p_aint 0: the cube drawn at this frame of
31⍝# the turn -- turning, or (at its end) with the turn made
32ᵘp̲aint ← { (c, i, f) z →
33 f < frames ? (c, i, (f̲loat f + 1) ÷ f̲loat frames) ʳᵇd̲raw w
34 f = frames ? (i ʳᵇt̲urn c, 0, 0.0) ʳᵇd̲raw w
35 0
36}
37
38⍝# (turn, frame) u:a_fter c: the colors after this frame: turned once,
39⍝# at the turn's end
40ᵘa̲fter ← { (i, f) c →
41 f = frames ? i ʳᵇt̲urn c
42 c
43}
44
45⍝# the turn about to be shown, printed (and the start of each half)
46ᵘa̲nnounce ← { yes j →
47 0 = yes ? 0
48 k ← 1 + (j − 1) m̲od t̲ally turns
49 i ← k s̲elect turns
50 said ← (k = 1) ᵘs̲ay "scrambling"
51 said ← (k = 1 + t̲ally scramble) ᵘs̲ay "solving"
52 said ← p̲rint! "turn " c̲at d̲isclose i s̲elect names
53 0
54}
55ᵘs̲ay ← { yes m →
56 0 = yes ? 0
57 said ← p̲rint! m
58 0
59}
60
61⍝# one frame of the show: the state is (colors, which turn, its frame,
62⍝# running)
63ᵘs̲how ← { (c, j, f, on) →
64 i ← (1 + (j − 1) m̲od t̲ally turns) s̲elect turns
65 said ← (f = 0) ᵘa̲nnounce j
66 drawn ← (c, i, f) ᵘp̲aint 0
67 c ← (i, f) ᵘa̲fter c
68 wait ← rest + pause × (0 = (j m̲od t̲ally scramble))
69 f < frames + wait ? (c, j, f + 1, on)
70 (c, j + 1, 0, on)
71}
72
73ᵘf̲rame ← { st →
74 (c, j, f, on) ← st
75 0 = on ? st
76 v ← ˢᶜn̲ext! w
77 ((v m̲atch "close") + v m̲atch "key q") > 0 ? ᵘq̲uit st
78 ᵘs̲how st
79}
80ᵘq̲uit ← { (c, j, f, on) →
81 z ← ˢᶜc̲lose! w
82 (c, j, f, 0)
83}
84shown ← (ʳᵇsolved, 0, 0.0) ʳᵇd̲raw w
85last ← 100000 'ᵘf̲rame p̲ower (ʳᵇsolved, 1, 0, 1)
86"bye"