@rotate.html


001| '''Rotation in 2D, 3D, and 4D Space.
001| 
001| Exploring the equation that governs point rotation in 2D and 3D.
001| (Part of an exploration into rotation versus "flipping" in up to five dimensions.)
001| 
001| 2D
001| ==
001| When we rotate a face on the XY plane, a point in the face moves in a circle.
001| 
001| Its XY coordinates change as:
001|     R * [cos(A0+A), sin(A0+A)]
001| 
001| Where R is radius and A0 is atan2(Y,X) and A is angle (in radians).
001| 
001| It can also be viewed as multiplication in the complex plane:
001|     (X, Yi) * (cos(A), sin(A)i)
001| 
001| Which is much simpler math than the full trig expression:
001|     [sqrt(X^2 + Y^2) * cos(atan2(Y,X)+A), sqrt(X^2 + Y^2) * sin(atan2(Y,X)+A)]
001| 
001| As with the complex plane expression, the only inputs here are X, Y, and A.
001| In algorithmic notation:
001|     sqrt(X^2+Y^2) => rad, atan2(Y,X)+A => ang; rad*[cos(ang),sin(ang)] => [X,Y]
001| 
001| 3D
001| ==
001| When we rotate in XYZ space, points still move in circles about the rotation axis.
001| Two of their coordinates change per [cos(A),sin(A)], while the third stays fixed.
001| Therefore, rotation on the X, Y, or Z, axis can be simplified to the complex plane.
001| 
001| For instance, rotating on the Y-axis (using trig) looks like this:;
001|     [R*cos(A0+A), Y, R*sin(A0+A)]
001| 
001| Using the complex plane:
001|     (X, Zi) * (cos(A), sin(A)i) => (X, Zi); [X, Y, Z]
001| 
001| Euler?
001| ======
001| 
001| '''

002| ####################################################################################################
003| from sys import argv
004| from os import path
005| from math import sqrt, sin,cos,atan2, radians,degrees
006| from vector import vector, V0,VX,VY,VZ, quaternion, Q0,QI,QJ,QK
007| from colors import ColorBlack, ColorWhite
008| from canvas import Canvas
009| from img3d import Img3D
010| from matrix import matrix, mvector2d,mvector3d, rotate2d, rotate3d_x,rotate3d_y,rotate3d_z
011| ####################################################################################################
012| BasePath = r'C:\CJS\prj\Python\app\maths\rotation'
013| ImgsPath = path.join(BasePath, 'img')
014| 
015| f_XY = lambda a: (cos(a),sin(a))
016| 
017| ##================================================================================================##
018| def do_canvas_test (fname='canvas.png', fpath=BasePath):
019|     dims = (500,500)
020|     xmin, xmax = float(-1.5), float(+1.5)
021|     ymin, ymax = float(-1.5), float(+1.5)
022|     view = ((xmin,xmax), (ymin,ymax))
023|     im = Canvas(dims, view)
024|     print('size=%s' % str(im.size))
025|     print('mode=%s' % str(im.mode))
026|     print('plot=%s' % str(im.view))
027|     print('wndw=%s' % str(im.window))
028|     print('fctr=%s' % str(im.factor))
029|     print()
030|     for x in [xmin*1.1, xmin, xmin*0.9, (xmax+xmin)/2.0, xmax*0.9, xmax, xmax*1.1]:
031|         print('x=%+.2f >> px=%d' % (x,im.px(x)))
032|     print()
033|     for y in [ymin*1.1, ymin, ymin*0.9, (ymax+ymin)/2.0, ymax*0.9, ymax, ymax*1.1]:
034|         print('y=%+.2f >> px=%d' % (y,im.py(y)))
035|     print()
036| 
037|     # Grid...
038|     im.grid(0.1, 0.1)
039|     im.unit_square(c=(63,63,63), lw=1)
040|     im.axis()
041|     im.border([ColorBlack], lw=2)
042| 
043|     # Stuff...
044|     im.circle((-0.5,-0.5), 0.5, bg=(159,255,191), lw=1)
045|     im.box((-0.25,-0.25), (-0.75,-0.75), bg=(159,191,255), lw=3)
046| 
047|     # Compass...
048|     angles = [radians(d) for d in range(0,360,5)]
049|     for a in angles:
050|         im.point9(f_XY(a), c=(255,0,0))
051| 
052|     im.save(path.join(fpath, fname))
053|     return 'Done!'
054| 
055| 
056| ##================================================================================================##
057| def rotate_2d_t (pt, angle):
058|     '''Rotate using trig.'''
059|     r = sqrt((pt.x**2)+(pt.y**2))
060|     a = radians(angle)+atan2(pt.y, pt.x)
061|     return vector(r*cos(a), r*sin(a))
062| ##================================================================================================##
063| def rotate_2d_c (pt, angle):
064|     '''Rotate using complex plane.'''
065|     cn = complex(*pt) * complex(*f_XY(radians(angle)))
066|     return vector(cn.real, cn.imag)
067| ##================================================================================================##
068| def rotate_2d_m (pt, angle):
069|     '''Rotate using a rotation matrix.'''
070|     mr = rotate2d(angle)
071|     mp = mvector2d(pt.x, pt.y)
072|     ma = mr.rotate(mp)
073|     return vector(ma.x, ma.y)
074| ##================================================================================================##
075| def do_rotate_2d (fname='rotate_2d.png', fpath=BasePath):
076|     dims = (500,500)
077|     xmin, xmax = float(-1.5), float(+1.5)
078|     ymin, ymax = float(-1.5), float(+1.5)
079|     view = ((xmin,xmax), (ymin,ymax))
080|     ang = 3
081|     segments = 45 // ang
082| 
083|     pa = vector(-1, +1)
084|     pb = vector(+1, +1)
085|     pc = vector(+1, -1)
086|     pd = vector(-1, -1)
087| 
088|     im = Canvas(dims, view)
089|     im.grid(0.1, 0.1, lw=1, c=(221,221,255))
090|     im.axis(lw=1, c=(95,95,95))
091|     im.point81((0,0))
092| 
093|     def linecolor (ix):
094|        v = float(ix+1) / float(segments)
095|        c = int(256 * v)
096|        n = 256 - c
097|        return (c,0,n)
098| 
099|     def drawit (sx):
100|         im.line(pa,pb, lw=2, c=linecolor(sx))
101|         im.line(pb,pc, lw=2, c=linecolor(sx))
102|         im.line(pc,pd, lw=2, c=linecolor(sx))
103|         im.line(pd,pa, lw=2, c=linecolor(sx))
104|         im.point13(pa, c=(0,0,0))
105|         im.point13(pb, c=(0,0,0))
106|         im.point13(pc, c=(0,0,0))
107|         im.point13(pd, c=(0,0,0))
108| 
109|     drawit(0)
110|     for sx in range(segments):
111|         pa = rotate_2d_m(pa, ang)
112|         pb = rotate_2d_m(pb, ang)
113|         pc = rotate_2d_m(pc, ang)
114|         pd = rotate_2d_m(pd, ang)
115|         drawit(sx)
116| 
117|     im.save(path.join(fpath, fname))
118|     return 'Done!'
119| 
120| 
121| ##================================================================================================##
122| def rotate_3d_t_x (pt, angle):
123|     '''Rotate on X-axis using trig.'''
124|     r = sqrt((pt.y**2)+(pt.z**2))
125|     a = radians(angle)+atan2(pt.z, pt.y)
126|     return vector(pt.x, r*cos(a), r*sin(a))
127| ##================================================================================================##
128| def rotate_3d_t_y (pt, angle):
129|     '''Rotate on Y-axis using trig.'''
130|     r = sqrt((pt.x**2)+(pt.z**2))
131|     a = radians(angle)+atan2(pt.z, pt.x)
132|     return vector(r*cos(a), pt.y, r*sin(a))
133| ##================================================================================================##
134| def rotate_3d_t_z (pt, angle):
135|     '''Rotate on Z-axis using trig.'''
136|     r = sqrt((pt.x**2)+(pt.y**2))
137|     a = radians(angle)+atan2(pt.y, pt.x)
138|     return vector(r*cos(a), r*sin(a), pt.z)
139| ##================================================================================================##
140| def rotate_3d_c_x (pt, angle):
141|     '''Rotate on X-axis using complex plane.'''
142|     cn = complex(pt.y, pt.z) * complex(*f_XY(radians(angle)))
143|     return vector(pt.x, cn.real, cn.imag)
144| ##================================================================================================##
145| def rotate_3d_c_y (pt, angle):
146|     '''Rotate on Y-axis using complex plane.'''
147|     cn = complex(pt.x, pt.z) * complex(*f_XY(radians(angle)))
148|     return vector(cn.real, pt.y, cn.imag)
149| ##================================================================================================##
150| def rotate_3d_c_z (pt, angle):
151|     '''Rotate on Z-axis using complex plane.'''
152|     cn = complex(pt.x, pt.y) * complex(*f_XY(radians(angle)))
153|     return vector(cn.real, cn.imag, pt.z)
154| ##================================================================================================##
155| def do_rotate_3d (fname='rotate_3d.png', fpath=BasePath):
156|     dims = (640,480)
157|     view = (20,20,13)
158|     im = Img3D(dims=dims, scale=view)
159|     im.unit_box(multiply=8)
160| 
161|     ang = 10
162|     p1 = vector(+6, -2, -0)
163|     p2 = vector(-9, -2, -0)
164|     for _ in range(360//ang):
165|         im.circle(p1, 2, c=(255,0,0))
166|         im.circle(p2, 2, c=(255,0,0))
167|         im.line(V0,p1, c=(127,0,0), lw=1)
168|         im.line(V0,p2, c=(127,0,0), lw=1)
169|         p1 = rotate_3d_t_x(p1, ang)
170|         p2 = rotate_3d_c_x(p2, ang)
171| 
172|     p1 = vector(-6, +9, -6)
173|     p2 = vector(-6, -5, -6)
174|     for _ in range(360//ang):
175|         im.circle(p1, 2, c=(0,127,0))
176|         im.circle(p2, 2, c=(0,127,0))
177|         im.line(V0,p1, c=(0,127,0), lw=1)
178|         im.line(V0,p2, c=(0,127,0), lw=1)
179|         p1 = rotate_3d_t_y(p1, ang)
180|         p2 = rotate_3d_c_y(p2, ang)
181| 
182|     p1 = vector(-1, +2, -20)
183|     p2 = vector(-2, +1, +20)
184|     for _ in range(360//ang):
185|         im.circle(p1, 2, c=(0,0,191))
186|         im.circle(p2, 2, c=(0,0,191))
187|         im.line(V0,p1, c=(0,0,191), lw=1)
188|         im.line(V0,p2, c=(0,0,191), lw=1)
189|         p1 = rotate_3d_t_z(p1, ang)
190|         p2 = rotate_3d_c_z(p2, ang)
191| 
192|     im.save(path.join(fpath, fname))
193|     return 'Done!'
194| 
195| 
196| ##================================================================================================##
197| def do_matrix_rotate (fname='matrix-rotate.png', fpath=BasePath):
198|     dims = (640,480)
199|     view = (100,100,33)
200|     ang = 10
201|     Mrx = rotate3d_x(ang)
202|     Mry = rotate3d_y(ang)
203|     Mrz = rotate3d_z(ang)
204| 
205|     im = Img3D(dims=dims, scale=view)
206|     im.unit_box()
207| 
208|     p0 = vector(0,0,0)
209|     px = mvector3d(+2, 0, +1.0)
210|     py = mvector3d(0, +1.5, +1.5)
211|     pz = mvector3d(0, 0.5, -8.0)
212|     for _ in range(360//ang):
213|         im.line(p0, px, c=(191,0,0), lw=1)
214|         im.line(p0, py, c=(0,191,0), lw=1)
215|         im.line(p0, pz, c=(0,0,191), lw=1)
216|         px = Mrx.rotate(px)
217|         py = Mry.rotate(py)
218|         pz = Mrz.rotate(pz)
219| 
220|     im.save(path.join(fpath, fname))
221|     return 'Done!'
222| 
223| 
224| ##================================================================================================##
225| def do_spirograph (fname='spirograph.png', fpath=BasePath):
226|     dims = (500,500)
227|     xmin, xmax = float(-5), float(+5)
228|     ymin, ymax = float(-5), float(+5)
229|     view = ((xmin,xmax), (ymin,ymax))
230| 
231|     # Grid...
232|     im = Canvas(dims, view)
233|     im.grid(0.5, 0.5)
234|     im.axis()
235|     im.border([ColorBlack], lw=2)
236| 
237|     # Stuff...
238|     rot = lambda ang: complex(cos(radians(ang)), sin(radians(ang)))
239|     wheel = lambda freq: lambda c: c*rot(freq)
240|     w1 = wheel(1)
241|     w2 = wheel(60)
242|     w3 = wheel(2)
243|     w4 = wheel(44)
244|     w5 = wheel(24)
245|     pt1 = complex(2.00, 0) * rot(0)
246|     pt2 = complex(0.10, 0) * rot(0)
247|     pt3 = complex(1.00, 0) * rot(0)
248|     pt4 = complex(0.25, 0) * rot(0)
249|     pt5 = complex(0.50, 0) * rot(0)
250| 
251|     pts = []
252|     for _ in range(0, 2000):
253|         pt1 = w1(pt1)
254|         pt2 = w2(pt2)
255|         pt3 = w3(pt3)
256|         pt4 = w4(pt4)
257|         pt5 = w5(pt5)
258|         pts.append(pt1+pt2+pt3+pt4+pt5)
259| 
260|     p = None
261|     for pt in pts:
262|         if p: im.line((p.real, p.imag), (pt.real, pt.imag), c=(255,0,0))
263|         p = pt
264| 
265|     im.save(path.join(fpath, fname))
266|     return 'Done!'
267| 
268| 
269| ##================================================================================================##
270| def do_test (*args):
271|     fn = path.join(BasePath, 'rotate.png')
272|     return 'Done!'
273| ####################################################################################################
274| def dispatch (cmd, *args):
275|     print('command: %s [#args: %d]' % (cmd,len(args)))
276|     print('***')
277|     if cmd == 'test': return do_test(*args)
278|     if cmd == '2d': return do_rotate_2d()
279|     if cmd == '3d': return do_rotate_3d()
280|     if cmd == 'm.rotate': return do_matrix_rotate()
281|     if cmd == 'canvas': return do_canvas_test(*args)
282|     if cmd == 'spiro': return do_spirograph()
283|     return 'Nothing to do!'
284| ####################################################################################################
285| if __name__ == '__main__':
286|     print('autorun: %s' % argv[0])
287|     cmd = argv[1] if 1 < len(argv) else 'spiro'
288|     etc = argv[2:] if 2 < len(argv) else []
289|     obj = dispatch(cmd, *etc)
290|     print('***')
291|     print(obj)
292|     print('***')
293|     print()
294| ####################################################################################################
295| '''eof'''
296|