Skip to content

Instantly share code, notes, and snippets.

@toomasv
Last active April 8, 2018 07:34
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save toomasv/800aaa6ba1a37526993b9f2fce52fd13 to your computer and use it in GitHub Desktop.
Memory-stable 3D cube
Red [
Author: "Toomas Vooglaid"
Date: 2018-04-07
Purpose: {Study of 3d rotations with ~stable mamory usage}
Needs: 'View
Last-update: 2018-04-08
]
system/view/auto-sync?: off
context [
mx: [1 0 0 0 c -s 0 s c]
my: [c 0 s 0 1 0 -s 0 c]
mz: [c -s 0 s c 0 0 0 1]
_m1: [0 0 0 0 0 0 0 0 0]
_m: [0 0 0 0 0 0 0 0 0]
points: [
[-50 -50 -50][50 -50 -50][50 50 -50][-50 50 -50][-50 -50 50][50 -50 50][50 50 50][-50 50 50]
[70 0 0][0 70 0][0 0 70]
]
pts: [-50x-50 50x-50 50x50 -50x50 -50x-50 50x-50 50x50 -50x50 70x0 0x70 0x0]
pts3: [0 0 0 0 0 0 0 0 0 0 0]
matrix: func [angle-x angle-y angle-z /origin orig][
mx/5: mx/9: cosine angle-x
mx/6: negate mx/8: sine angle-x
my/1: my/9: cosine angle-y
my/7: negate my/3: sine angle-y
mz/1: mz/5: cosine angle-z
mz/2: negate mz/4: sine angle-z
_m1/1: (mx/1 * my/1) + (mx/2 * my/4) + (mx/3 * my/7)
_m1/2: (mx/1 * my/2) + (mx/2 * my/5) + (mx/3 * my/8)
_m1/3: (mx/1 * my/3) + (mx/2 * my/6) + (mx/3 * my/9)
_m1/4: (mx/4 * my/1) + (mx/5 * my/4) + (mx/6 * my/7)
_m1/5: (mx/4 * my/2) + (mx/5 * my/5) + (mx/6 * my/8)
_m1/6: (mx/4 * my/3) + (mx/5 * my/6) + (mx/6 * my/9)
_m1/7: (mx/7 * my/1) + (mx/8 * my/4) + (mx/9 * my/7)
_m1/8: (mx/7 * my/2) + (mx/8 * my/5) + (mx/9 * my/8)
_m1/9: (mx/7 * my/3) + (mx/8 * my/6) + (mx/9 * my/9)
_m/1: (_m1/1 * mz/1) + (_m1/2 * mz/4) + (_m1/3 * mz/7)
_m/2: (_m1/1 * mz/2) + (_m1/2 * mz/5) + (_m1/3 * mz/8)
_m/3: (_m1/1 * mz/3) + (_m1/2 * mz/6) + (_m1/3 * mz/9)
_m/4: (_m1/4 * mz/1) + (_m1/5 * mz/4) + (_m1/6 * mz/7)
_m/5: (_m1/4 * mz/2) + (_m1/5 * mz/5) + (_m1/6 * mz/8)
_m/6: (_m1/4 * mz/3) + (_m1/5 * mz/6) + (_m1/6 * mz/9)
_m/7: (_m1/7 * mz/1) + (_m1/8 * mz/4) + (_m1/9 * mz/7)
_m/8: (_m1/7 * mz/2) + (_m1/8 * mz/5) + (_m1/9 * mz/8)
_m/9: (_m1/7 * mz/3) + (_m1/8 * mz/6) + (_m1/9 * mz/9)
]
rotate-points: does [
forall points [
pts/(index? points): as-pair
round/to (_m/1 * points/1/1) + (_m/2 * points/1/2) + (_m/3 * points/1/3) 1.0
round/to (_m/4 * points/1/1) + (_m/5 * points/1/2) + (_m/6 * points/1/3) 1.0
pts3/(index? points): (_m/7 * points/1/1) + (_m/8 * points/1/2) + (_m/9 * points/1/3)
]
]
rotate: func [angle-x angle-y angle-z /origin orig][
matrix angle-x angle-y angle-z
rotate-points
]
adjust: does [
pg1/2: pg2/2: pg5/2: pts/1
pg1/3: pg4/2: pg5/5: pts/2
pg1/4: pg4/5: pg6/5: pts/3
pg1/5: pg2/5: pg6/2: pts/4
pg2/3: pg3/2: pg5/3: pts/5
pg3/3: pg4/3: pg5/4: pts/6
pg3/4: pg4/4: pg6/4: pts/7
pg2/4: pg3/5: pg6/3: pts/8
x-axis/3: x-axis/5: pts/9
y-axis/3: y-axis/5: pts/10
z-axis/3: z-axis/5: pts/11
if all [pts3/1 + pts3/3 / 2.0 < 0 3 < idx: index? find p/pane s1][move at p/pane idx head p/pane]
if all [pts3/1 + pts3/8 / 2.0 < 0 3 < idx: index? find p/pane s2][move at p/pane idx head p/pane]
if all [pts3/5 + pts3/7 / 2.0 < 0 3 < idx: index? find p/pane s3][move at p/pane idx head p/pane]
if all [pts3/2 + pts3/7 / 2.0 < 0 3 < idx: index? find p/pane s4][move at p/pane idx head p/pane]
if all [pts3/1 + pts3/6 / 2.0 < 0 3 < idx: index? find p/pane s5][move at p/pane idx head p/pane]
if all [pts3/4 + pts3/7 / 2.0 < 0 3 < idx: index? find p/pane s6][move at p/pane idx head p/pane]
show p
]
view compose/deep [
b: button "Go" [
either face/text = "Stop" [
p/rate: none face/text: "Go"
][
p/rate: r/data face/text: "Stop"
]
show [b p]
]
text 40x24 right "Rate:" r: field 40x24 "10" [p/rate: face/data]
t: text "" return
style chk: check 25x24
style slid: slider 270x24 data 0.0 [
rotate _x/data * 360 _y/data * 360 _z/data * 360
adjust
t/text: form round/to face/data * 360 .1
show t
]
_x1: chk "X:" _x: slid return
_y1: chk "Y:" _y: slid return
_z1: chk "Z:" _z: slid return
p: panel 300x300 [
at 0x0 s1: box 300x300 draw [
translate 150x150
fill-pen black
pg1: polygon (pts/1) (pts/2) (pts/3) (pts/4)
]
at 0x0 s2: box 300x300 draw [
translate 150x150
fill-pen red
pg2: polygon (pts/1) (pts/5) (pts/8) (pts/4)
]
at 0x0 s3: box 300x300 draw [
translate 150x150
fill-pen green
pg3: polygon (pts/5) (pts/6) (pts/7) (pts/8)
]
at 0x0 s4: box 300x300 draw [
translate 150x150
fill-pen blue
pg4: polygon (pts/2) (pts/6) (pts/7) (pts/3)
]
at 0x0 s5: box 300x300 draw [
translate 150x150
fill-pen yellow
pg5: polygon (pts/1) (pts/5) (pts/6) (pts/2)
]
at 0x0 s6: box 300x300 draw [
translate 150x150
fill-pen brown
pg6: polygon (pts/4) (pts/8) (pts/7) (pts/3)
]
at 0x0 crd: box 300x300 draw [
translate 150x150
pen magenta
x-axis: line 0x0 (pts/9) text (pts/9) "X"
y-axis: line 0x0 (pts/10) text (pts/10) "Y"
z-axis: line 0x0 (pts/11) text (pts/11) "Z"
]
]
on-time [
if all [b/text = "Stop" any [_x1/data _y1/data _z1/data]][
if _x1/data [_x/data: _x/data + 0.01 % 1.0]
if _y1/data [_y/data: _y/data + 0.01 % 1.0]
if _z1/data [_z/data: _z/data + 0.01 % 1.0]
show [_x _y _z]
rotate _x/data * 360 _y/data * 360 _z/data * 360
adjust
]
]
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment