here2share

# Tk_3DsphereXspin.py

Mar 9th, 2021 (edited)
991
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.43 KB | None | 0 0
  1. # Tk_3DsphereXspin.py ZZZ + auto_mesh
  2.  
  3. from math import cos,sin,pi,radians,sqrt
  4. import random
  5.  
  6. r=random.randint
  7.  
  8. try:
  9.     import tkinter as tk
  10. except:
  11.     import Tkinter as tk
  12.  
  13. root=tk.Tk()
  14. root.title("Tk 3D Sphere Spin")
  15.  
  16. ww=640
  17. hh=640
  18. xc=ww/2
  19. yc=hh/2
  20.  
  21. halfResX = ww/2
  22. halfResY = hh/2
  23.  
  24. canvas=tk.Canvas(root,width=ww,height=hh)
  25. canvas.pack(fill="both",expand=True)
  26.  
  27. COLORS='red orange yellow green lightblue purple'.split()
  28.  
  29. colors2sides=[]
  30. for z in range(4,60):
  31.     for i in (3,4,5,7):
  32.         if not z%i:
  33.             colors2sides += [(z,i)]
  34.             break
  35. colors2sides=dict(colors2sides)
  36.  
  37. class Cv():
  38.     x,y,z=0,0,0
  39.     xx,yy=0,0
  40.     ANGLEX = 0
  41.     ANGLEY = 0
  42.     ANGLEZ = 0
  43.     PZ = 200
  44.     xm,ym,x2,y2 = 3,2,0,0
  45. cv=Cv()
  46.  
  47. def click(event):
  48.     cv.xx,cv.yy = (event.x,event.y)
  49.  
  50. def drag(event):
  51.     tx = (event.x-cv.xx)
  52.     ty = (event.y-cv.yy)
  53.    
  54.     cv.xm += tx
  55.     cv.ym += ty
  56.    
  57.     cv.xm %= 360
  58.     cv.ym %= 360
  59.     cv.xx,cv.yy = (event.x,event.y)
  60.  
  61. canvas.bind('<Button-1>',click)
  62. canvas.bind('<B1-Motion>',drag)
  63.  
  64. distance = 400
  65.  
  66. x1, y1, x2, y2=200, 0, 400, 500
  67. angle=0
  68. speed=10
  69. x=y=z=10
  70. sides=7
  71.  
  72.  
  73. XYZ = {}
  74. def fibonacci_sphere(points=4):
  75.     POINTS = []
  76.     C = 0
  77.     phi = pi*(3.-sqrt(5.))  # golden angle in radians
  78.  
  79.     for i in range(points):
  80.         y = 1-(i/(points - 1.0))*2  # y goes from 1 to -1
  81.         radius = sqrt(1-y*y)  # radius at y
  82.  
  83.         theta = phi*i  # golden angle increment
  84.  
  85.         x = cos(theta)*radius
  86.         z = sin(theta)*radius
  87.  
  88.         POINTS.append((x,y,z))
  89.         XYZ[x,y,z] = C
  90.     return POINTS
  91. points = fibonacci_sphere(100)
  92.  
  93. def fn_distance(objA, objB):  
  94.     x1, y1, z1 = objA
  95.     x2, y2, z2 = objB
  96.     d = ((x2 - x1)**2 + (y2 - y1)**2 + (z2 - z1)**2)**0.333333
  97.     return d
  98.  
  99. EDGES = {}
  100. SIDES = []
  101. for objA in points:
  102.     t = []
  103.     pts = points[:]
  104.     pts.remove(objA)
  105.     for objB in pts:
  106.         t += [(fn_distance(objA, objB), objB)]
  107.     t.sort(reverse=0)
  108.     while len(t) > 1:
  109.         a = t.pop()
  110.         b = t.pop()
  111.         ab = fn_distance(a[1],b[1])
  112.         x,y,z = sorted([a[1],b[1],objA])
  113.         v1 = min(a[0],b[0],ab)
  114.         v2 = max(a[0],b[0],ab)
  115.         if (v2 < 0.7):
  116.             if (x,y,z) not in SIDES:
  117.                 SIDES += [(x,y,z)]
  118. print len(SIDES)
  119. SIDES.sort()
  120. go=1
  121. Lc=len(COLORS)
  122.  
  123. c = 0
  124. sideColor = {}
  125. for t in SIDES:
  126.     sideColor[t] = COLORS[c%Lc]
  127.     c += 1
  128.  
  129. def animate(vradius=100):
  130.     while 1:
  131.         canvas.delete('all')
  132.        
  133.         rax = cv.ANGLEX*pi/180
  134.         ray = cv.ANGLEY*pi/180
  135.      
  136.         sinx = sin(rax)
  137.         cosx = cos(rax)
  138.         siny = sin(ray)
  139.         cosy = cos(ray)
  140.         r1 = []
  141.         r2 = []
  142.         xy = []
  143.        
  144.         for side in SIDES:
  145.             t = []
  146.             zt = []
  147.             color = sideColor[side]
  148.             for vectors in side:
  149.                 VX,VY,VZ = [z*vradius for z in vectors]
  150.                 YX = VX*cosy - VZ*siny - VX
  151.                 YZ = VX*siny + VZ*cosy - VZ
  152.                 XY = VY*cosx - (VZ+YZ)*sinx - VY
  153.                 XZ = VY*sinx + (VZ+YZ)*cosx - (VZ+YZ)
  154.                 ZR = XZ+YZ
  155.                 z = (VZ+cv.PZ+ZR)/distance
  156.                 x = ((VX+YX)/z)+halfResX
  157.                 y = ((VY+XY)/z)+halfResY
  158.                 t.append([x,y])
  159.                 zt.append(z)
  160.             xy.append([max(zt),t,color,(x,y)])
  161.        
  162.         xy.sort(reverse=1)
  163.        
  164.         dots = []
  165.         for z,side,color,dot in xy:
  166.             canvas.create_polygon(side,fill=color,tag='ball_A')
  167.             dots += [dot]
  168.         L = len(dots)
  169.         for x,y in dots[int(L*0.7):]:
  170.             canvas.create_oval(x,y,x+5,y+5,fill='black',tag='dots')
  171.            
  172.         cv.ANGLEX = cv.ym
  173.         cv.ANGLEY = cv.xm
  174.        
  175.         canvas.create_text((30,20),text='Drag Mouse To Rotate 3D Object',anchor='w')
  176.         canvas.create_text((30,40),text='X: %d'%(cv.xm),anchor='w')
  177.         canvas.create_text((30,60),text='Y: %d'%(cv.ym),anchor='w')
  178.        
  179.         break
  180.     root.after(60, animate)
  181.  
  182. animate()
Add Comment
Please, Sign In to add comment