Advertisement
here2share

# Tk_3DcubeXspin.py

Dec 3rd, 2020 (edited)
1,055
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.28 KB | None | 0 0
  1. # Tk_3DcubeXspin.py
  2.  
  3. # drag-to-rotate
  4.  
  5. import math,time,os,sys
  6.  
  7. from Tkinter import *
  8. from PIL import ImageTk, Image
  9.  
  10. w=640
  11. h=640
  12. Size = 10
  13.  
  14. class Cv():
  15.     x,y,z=0,0,0
  16.     xx,yy=0,0
  17.     objv=False
  18.     ANGLEX = 0
  19.     ANGLEY = 0
  20.     ANGLEZ = 0
  21.     PX = 0
  22.     PY = 0
  23.     PZ = 200
  24.     xm,ym,x2,y2 = 3,2,0,0
  25.     rx=1
  26.     ry=1
  27.     run=1
  28. cv=Cv()
  29.  
  30. halfResX = w/2
  31. halfResY = h/2
  32.  
  33. def click(event):
  34.     cv.xx,cv.yy = (event.x,event.y)
  35.  
  36. def drag(event):
  37.     tx = (event.x-cv.xx)
  38.     ty = (event.y-cv.yy)
  39.    
  40.     cv.xm += tx
  41.     cv.ym += ty
  42.    
  43.     cv.xm %= 360
  44.     cv.ym %= 360
  45.     cv.xx,cv.yy = (event.x,event.y)
  46.  
  47. root = Tk()
  48. canvas = Canvas(root,width=w,height=h)
  49. canvas.pack()
  50.  
  51. canvas.bind('<Button-1>',click)
  52. canvas.bind('<B1-Motion>',drag)
  53.  
  54. distance = 400 # object size to create distance illusion
  55.  
  56. xyz=80
  57. VX = [-xyz, -xyz, -xyz, -xyz,  xyz,  xyz,  xyz,  xyz]
  58. VY = [-xyz,  xyz, -xyz,  xyz, -xyz,  xyz, -xyz,  xyz]
  59. VZ = [-xyz, -xyz,  xyz,  xyz, -xyz, -xyz,  xyz,  xyz]
  60.  
  61. 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)]
  62.  
  63. colors = 'red darkorange yellow green blue purple'.split()
  64.  
  65. def animate():
  66.     while cv.run:
  67.         rax = cv.ANGLEX*math.pi/180
  68.         ray = cv.ANGLEY*math.pi/180
  69.      
  70.         sinx = math.sin(rax)
  71.         cosx = math.cos(rax)
  72.         siny = math.sin(ray)
  73.         cosy = math.cos(ray)
  74.         r1 = []
  75.         r2 = []
  76.        
  77.         for n in range(8):
  78.             YX = VX[n]*cosy - VZ[n]*siny - VX[n]
  79.             YZ = VX[n]*siny + VZ[n]*cosy - VZ[n]
  80.             XY = VY[n]*cosx - (VZ[n]+YZ)*sinx - VY[n]
  81.             XZ = VY[n]*sinx + (VZ[n]+YZ)*cosx - (VZ[n]+YZ)
  82.             XR = YX
  83.             YR = XY
  84.             ZR = XZ+YZ
  85.             z = (VZ[n]+cv.PZ+ZR)/distance
  86.             x = ((VX[n]+cv.PX+XR)/z)+halfResX
  87.             y = ((VY[n]+cv.PY+YR)/z)+halfResY
  88.             r1.append([x,y])
  89.             r2.append(z)
  90.  
  91.  
  92.         canvas.delete('all')
  93.                
  94.         t = []
  95.         c = 0
  96.         for f in faces:
  97.             z1 = [r1[z] for z in f]
  98.             z2 = [r2[z] for z in f]
  99.             t.append([max(z2),z1,colors[c]])
  100.             c += 1
  101.         z = False
  102.         if time.time() % 0.9 > 0.45: z = True
  103.         t.sort(reverse=z)
  104.         for c,a,b in t:
  105.             canvas.create_polygon(a,fill=b)
  106.            
  107.         cv.ANGLEX = cv.ym
  108.         cv.ANGLEY = cv.xm
  109.        
  110.         canvas.create_text((30,20),text='Drag Mouse To Rotate Cube',anchor=W)
  111.         canvas.create_text((30,40),text='X: %d'%(cv.xm),anchor=W)
  112.         canvas.create_text((30,60),text='Y: %d'%(cv.ym),anchor=W)
  113.        
  114.         break
  115.     root.after(60, animate)
  116.  
  117. animate()
  118. root.mainloop()
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement