Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_and_py60_3DcubeXspin_v2.py
- # drag-to-rotate
- import math,time,os,sys
- import thread
- w=500
- h=500
- Size = 10
- class Cv():
- x,y,z=0,0,0
- xx,yy=0,0
- objv=False
- ANGLEX = 0
- ANGLEY = 0
- ANGLEZ = 0
- PX = 0
- PY = 0
- PZ = 200
- xm,ym,x2,y2 = 3,2,0,0
- rx=1
- ry=1
- run=1
- cv=Cv()
- halfResX = w/2
- halfResY = h/2
- def draw(rect):
- if img: c.blit(img)
- def redraw():
- 0
- def scr_dn(event): ###ZZZ
- 0
- def click(event):
- cv.xx,cv.yy = (event.x,event.y)
- def drag(event):
- cv.xm += (event.x-cv.xx)
- cv.ym += (event.y-cv.yy)
- cv.xm %= 360
- cv.ym %= 360
- cv.xx,cv.yy = (event.x,event.y)
- s60=0
- try:
- import appuifw
- s60=1
- except: 0
- if s60:
- import e32,graphics,key_codes,sensor
- img=''
- appuifw.app.directional_pad=False
- appuifw.app.orientation='portrait'
- appuifw.app.screen='large'
- c=appuifw.Canvas(redraw_callback=draw)
- appuifw.app.body=c
- img=graphics.Image.new(c.size)
- c.bind(key_codes.EButton1Down,scr_dn)
- sens=sensor.AccelerometerXYZAxisData(data_filter=sensor.LowPassFilter())
- sens.set_callback(data_callback=redraw)
- sens.start_listening()
- halfResX = c.size[0]/2
- halfResY = c.size[1]/2
- def quit():
- cv.run=0
- app_lock.signal()
- appuifw.app.exit_key_handler=quit
- app_lock=e32.Ao_lock()
- else:
- from Tkinter import *
- from PIL import ImageTk, Image
- root = Tk()
- canvas = Canvas(root,width=w,height=h)
- canvas.pack()
- canvas.bind('<Button-1>',click)
- canvas.bind('<B1-Motion>',drag)
- distance = 180
- xyz=80
- VX = [-xyz, -xyz, -xyz, -xyz, xyz, xyz, xyz, xyz]
- VY = [-xyz, xyz, -xyz, xyz, -xyz, xyz, -xyz, xyz]
- VZ = [-xyz, -xyz, xyz, xyz, -xyz, -xyz, xyz, xyz]
- faces = [(0,1,3,2),(7,5,4,6),(0,2,6,4),(7,3,1,5),(7,3,2,6),(0,1,5,4)]
- colors = 'red darkorange yellow green blue violet'.split()
- if s60: colors = (255,0,255),(255,0,0),(0,255,0),(0,0,255),(0,255,255),(255,255,0)
- def animate():
- while cv.run:
- wait=time.time()+0.4
- rax = cv.ANGLEX*math.pi/180
- ray = cv.ANGLEY*math.pi/180
- raz = cv.ANGLEZ*math.pi/180
- sinx = math.sin(rax)
- cosx = math.cos(rax)
- siny = math.sin(ray)
- cosy = math.cos(ray)
- sinz = math.sin(raz)
- cosz = math.cos(raz)
- r1 = []
- r2 = []
- for n in range(8):
- ZX = VX[n]*cosz - VY[n]*sinz - VX[n]
- ZY = VX[n]*sinz + VY[n]*cosz - VY[n]
- YX = (VX[n]+ZX)*cosy - VZ[n]*siny - (VX[n]+ZX)
- YZ = (VX[n]+ZX)*siny + VZ[n]*cosy - VZ[n]
- XY = (VY[n]+ZY)*cosx - (VZ[n]+YZ)*sinx - (VY[n]+ZY)
- XZ = (VY[n]+ZY)*sinx + (VZ[n]+YZ)*cosx - (VZ[n]+YZ)
- XR = YX+ZX
- YR = ZY+XY
- ZR = XZ+YZ
- z = (VZ[n]+cv.PZ+ZR)/distance
- x = ((VX[n]+cv.PX+XR)/z)+halfResX
- y = ((VY[n]+cv.PY+YR)/z)+halfResY
- z = int(20/z)
- r1.append([x,y])
- r2.append(z)
- if s60:
- img.clear()
- img.text((20,40),'X:'+unicode(cv.xm))
- img.text((20,80),'Y:'+unicode(cv.ym))
- else:
- canvas.delete('all')
- canvas.create_text((60,40),text='X: %d'%(cv.xm))
- canvas.create_text((60,60),text='Y: %d'%(cv.ym))
- canvas.create_text((60,80),text='Z: %d'%(z))
- t = []
- c = 0
- for f in faces:
- z1 = [r1[z] for z in f]
- z2 = [r2[z] for z in f]
- t.append([sum(z2),z1,colors[c]])
- c += 1
- z = False
- if s60:
- if time.time() % 2 > 1: z = True
- else:
- if time.time() % 0.9 > 0.4: z = True
- t.sort(reverse=z)
- for c,a,b in t:
- if s60:
- img.polygon(a,fill=b)
- else:
- canvas.create_polygon(a,fill=b)
- cv.ANGLEX = cv.ym
- cv.ANGLEY = cv.xm
- if s60:
- draw(())
- e32.reset_inactivity()
- while time.time() < wait:
- 0
- e32.ao_yield()
- else: break
- if not s60:
- root.after(60, animate)
- animate()
- if s60:
- app_lock.wait()
- else:
- root.mainloop()
- z
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement