Advertisement
here2share

# Tk_and_py60_3DcubeXspin.py ^ 2018.10

Jul 23rd, 2017
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Tk_and_py60_3DcubeXspin_v2.py
  2.  
  3. # drag-to-rotate
  4.  
  5. import math,time,os,sys
  6. import thread
  7.  
  8. w=500
  9. h=500
  10. Size = 10
  11.  
  12. class Cv():
  13.     x,y,z=0,0,0
  14.     xx,yy=0,0
  15.     objv=False
  16.     ANGLEX = 0
  17.     ANGLEY = 0
  18.     ANGLEZ = 0
  19.     PX = 0
  20.     PY = 0
  21.     PZ = 200
  22.     xm,ym,x2,y2 = 3,2,0,0
  23.     rx=1
  24.     ry=1
  25.     run=1
  26. cv=Cv()
  27.  
  28. halfResX = w/2
  29. halfResY = h/2
  30.  
  31. def draw(rect):
  32.     if img: c.blit(img)
  33.  
  34. def redraw():
  35.     0
  36.  
  37. def scr_dn(event): ###ZZZ
  38.     0
  39.  
  40. def click(event):
  41.     cv.xx,cv.yy = (event.x,event.y)
  42.  
  43. def drag(event):
  44.     cv.xm += (event.x-cv.xx)
  45.     cv.ym += (event.y-cv.yy)
  46.     cv.xm %= 360
  47.     cv.ym %= 360
  48.     cv.xx,cv.yy = (event.x,event.y)
  49.  
  50. s60=0
  51. try:
  52.     import appuifw
  53.     s60=1
  54. except: 0
  55. if s60:
  56.     import e32,graphics,key_codes,sensor
  57.    
  58.     img=''
  59.     appuifw.app.directional_pad=False
  60.     appuifw.app.orientation='portrait'
  61.     appuifw.app.screen='large'
  62.     c=appuifw.Canvas(redraw_callback=draw)
  63.     appuifw.app.body=c
  64.     img=graphics.Image.new(c.size)
  65.     c.bind(key_codes.EButton1Down,scr_dn)
  66.     sens=sensor.AccelerometerXYZAxisData(data_filter=sensor.LowPassFilter())
  67.     sens.set_callback(data_callback=redraw)
  68.     sens.start_listening()
  69.  
  70.     halfResX = c.size[0]/2
  71.     halfResY = c.size[1]/2
  72.  
  73.     def quit():
  74.         cv.run=0
  75.         app_lock.signal()
  76.     appuifw.app.exit_key_handler=quit
  77.     app_lock=e32.Ao_lock()
  78.    
  79. else:
  80.     from Tkinter import *
  81.     from PIL import ImageTk, Image
  82.    
  83.     root = Tk()
  84.     canvas = Canvas(root,width=w,height=h)
  85.     canvas.pack()
  86.  
  87.     canvas.bind('<Button-1>',click)
  88.     canvas.bind('<B1-Motion>',drag)
  89.  
  90. distance = 180
  91.  
  92. xyz=80
  93. VX = [-xyz, -xyz, -xyz, -xyz,  xyz,  xyz,  xyz,  xyz]
  94. VY = [-xyz,  xyz, -xyz,  xyz, -xyz,  xyz, -xyz,  xyz]
  95. VZ = [-xyz, -xyz,  xyz,  xyz, -xyz, -xyz,  xyz,  xyz]
  96.  
  97. 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)]
  98.  
  99. colors = 'red darkorange yellow green blue violet'.split()
  100.  
  101. if s60: colors = (255,0,255),(255,0,0),(0,255,0),(0,0,255),(0,255,255),(255,255,0)
  102.  
  103. def animate():
  104.     while cv.run:
  105.         wait=time.time()+0.4
  106.         rax = cv.ANGLEX*math.pi/180
  107.         ray = cv.ANGLEY*math.pi/180
  108.         raz = cv.ANGLEZ*math.pi/180
  109.      
  110.         sinx = math.sin(rax)
  111.         cosx = math.cos(rax)
  112.         siny = math.sin(ray)
  113.         cosy = math.cos(ray)
  114.         sinz = math.sin(raz)
  115.         cosz = math.cos(raz)
  116.         r1 = []
  117.         r2 = []
  118.        
  119.         for n in range(8):
  120.             ZX = VX[n]*cosz - VY[n]*sinz - VX[n]
  121.             ZY = VX[n]*sinz + VY[n]*cosz - VY[n]
  122.             YX = (VX[n]+ZX)*cosy - VZ[n]*siny - (VX[n]+ZX)
  123.             YZ = (VX[n]+ZX)*siny + VZ[n]*cosy - VZ[n]
  124.             XY = (VY[n]+ZY)*cosx - (VZ[n]+YZ)*sinx - (VY[n]+ZY)
  125.             XZ = (VY[n]+ZY)*sinx + (VZ[n]+YZ)*cosx - (VZ[n]+YZ)
  126.             XR = YX+ZX
  127.             YR = ZY+XY
  128.             ZR = XZ+YZ
  129.             z = (VZ[n]+cv.PZ+ZR)/distance
  130.             x = ((VX[n]+cv.PX+XR)/z)+halfResX
  131.             y = ((VY[n]+cv.PY+YR)/z)+halfResY
  132.             z = int(20/z)
  133.             r1.append([x,y])
  134.             r2.append(z)
  135.  
  136.         if s60:
  137.             img.clear()
  138.             img.text((20,40),'X:'+unicode(cv.xm))
  139.             img.text((20,80),'Y:'+unicode(cv.ym))
  140.         else:
  141.             canvas.delete('all')
  142.             canvas.create_text((60,40),text='X: %d'%(cv.xm))
  143.             canvas.create_text((60,60),text='Y: %d'%(cv.ym))
  144.            
  145.             canvas.create_text((60,80),text='Z: %d'%(z))
  146.         t = []
  147.         c = 0
  148.         for f in faces:
  149.             z1 = [r1[z] for z in f]
  150.             z2 = [r2[z] for z in f]
  151.             t.append([sum(z2),z1,colors[c]])
  152.             c += 1
  153.         z = False
  154.         if s60:
  155.             if time.time() % 2 > 1: z = True
  156.         else:
  157.             if time.time() % 0.9 > 0.4: z = True
  158.         t.sort(reverse=z)
  159.         for c,a,b in t:
  160.             if s60:
  161.                 img.polygon(a,fill=b)
  162.             else:
  163.                 canvas.create_polygon(a,fill=b)
  164.            
  165.         cv.ANGLEX = cv.ym
  166.         cv.ANGLEY = cv.xm
  167.        
  168.         if s60:
  169.             draw(())
  170.             e32.reset_inactivity()
  171.             while time.time() < wait:
  172.                 0
  173.             e32.ao_yield()
  174.         else: break
  175.     if not s60:
  176.         root.after(60, animate)
  177.  
  178. animate()
  179. if s60:
  180.     app_lock.wait()
  181. else:
  182.     root.mainloop()
  183.  
  184. z
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement