Advertisement
here2share

# Tk_3DsphereXspin2.py

Mar 12th, 2021 (edited)
1,229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.61 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. class Cv():
  30.     x,y,z=0,0,0
  31.     xx,yy=0,0
  32.     ANGLEX = 0
  33.     ANGLEY = 0
  34.     ANGLEZ = 0
  35.     PZ = 200
  36.     xm,ym,x2,y2 = 3,2,0,0
  37. cv=Cv()
  38.  
  39. def click(event):
  40.     cv.xx,cv.yy = (event.x,event.y)
  41.  
  42. def drag(event):
  43.     tx = (event.x-cv.xx)
  44.     ty = (event.y-cv.yy)
  45.    
  46.     cv.xm += tx
  47.     cv.ym += ty
  48.    
  49.     cv.xm %= 360
  50.     cv.ym %= 360
  51.     cv.xx,cv.yy = (event.x,event.y)
  52.  
  53. canvas.bind('<Button-1>',click)
  54. canvas.bind('<B1-Motion>',drag)
  55.  
  56. distance = 400
  57.  
  58. x1, y1, x2, y2=200, 0, 400, 500
  59. angle=0
  60. speed=10
  61. x=y=z=10
  62. sides=7
  63.  
  64.  
  65. XYZ = {}
  66. def fibonacci_sphere(points=4):
  67.     POINTS = []
  68.     C = 0
  69.     phi = pi*(3.-5.**.5)  # golden angle in radians
  70.  
  71.     for i in range(points):
  72.         y = 1-(i/(points - 1.))*2  # y goes from 1 to -1
  73.         radius = sqrt(1-y*y)  # radius at y
  74.  
  75.         theta = phi*i  # golden angle increment
  76.  
  77.         x = cos(theta)*radius
  78.         z = sin(theta)*radius
  79.  
  80.         POINTS.append((x,y,z))
  81.         XYZ[x,y,z] = C
  82.     return POINTS
  83. points = fibonacci_sphere(100)
  84. points.pop(0)
  85.  
  86. def fn_distance(objA, objB):  
  87.     x1, y1, z1 = objA
  88.     x2, y2, z2 = objB
  89.     d = ((x2 - x1)**2 + (y2 - y1)**2 + (z2 - z1)**2)**0.333333
  90.     return d
  91.  
  92. SIDES = []
  93. L = len(points) - 1
  94.  
  95. for y in range(L):
  96.     for x in range(y,min(L,y+16)):
  97.         for z in range(x,min(L,x+16)):
  98.             ttt = [fn_distance(points[a],points[b]) for a,b in [(x,y),(x,z),(y,z)]]
  99.             ttt.sort()
  100.             if all(ttt):
  101.                 t = ttt[0]/ttt[2]
  102.                 if t > 0.69 and ttt[2] < 0.64:
  103.                     t = tuple(sorted([points[x],points[y],points[z]]))
  104.                     if t not in SIDES:
  105.                         SIDES += [t]
  106.  
  107. print len(SIDES)
  108. SIDES.sort()
  109.  
  110. RGBs = []
  111. def rgb_to_hex(rgb):
  112.     return '#%02x%02x%02x' % rgb
  113. def z():
  114.     RGBs.append(rgb_to_hex((r,g,b)))
  115. r,g,b = 255,0,0
  116. for g in range(256):
  117.     z()
  118. for r in range(254, -1, -1):
  119.     z()
  120. for b in range(256):
  121.     z()
  122. for g in range(254, -1, -1):
  123.     z()
  124. for r in range(256):
  125.     z()
  126. for b in range(254, -1, -1):
  127.     z()
  128.  
  129. Lc = len(RGBs)
  130.  
  131. c = 0
  132. sideColor = {}
  133. for t in SIDES:
  134.     sideColor[t] = RGBs[c%Lc]
  135.     c += 30
  136.  
  137. def animate(vradius=100):
  138.     while 1:
  139.         canvas.delete('all')
  140.        
  141.         rax = cv.ANGLEX*pi/180
  142.         ray = cv.ANGLEY*pi/180
  143.      
  144.         sinx = sin(rax)
  145.         cosx = cos(rax)
  146.         siny = sin(ray)
  147.         cosy = cos(ray)
  148.         r1 = []
  149.         r2 = []
  150.         xy = []
  151.        
  152.         for side in SIDES:
  153.             t = []
  154.             zt = []
  155.             color = sideColor[side]
  156.            
  157.             for vectors in side:
  158.                 VX,VY,VZ = [z*vradius for z in vectors]
  159.                 YX = VX*cosy - VZ*siny - VX
  160.                 YZ = VX*siny + VZ*cosy - VZ
  161.                 XY = VY*cosx - (VZ+YZ)*sinx - VY
  162.                 XZ = VY*sinx + (VZ+YZ)*cosx - (VZ+YZ)
  163.                 ZR = XZ+YZ
  164.                 z = (VZ+cv.PZ+ZR)/distance
  165.                 x = ((VX+YX)/z)+halfResX
  166.                 y = ((VY+XY)/z)+halfResY
  167.                 t.append([x,y])
  168.                 zt.append(z)
  169.             xy.append([max(zt),t,color,(x,y)])
  170.        
  171.         xy.sort(reverse=1)
  172.        
  173.         dots = []
  174.         for z,side,color,dot in xy:
  175.             canvas.create_polygon(side,fill=color,tag='ball_A')
  176.             dots += [dot]
  177.         L = len(dots)
  178.         for x,y in dots[int(L*0.7):]:
  179.             canvas.create_oval(x,y,x+5,y+5,fill='black',tag='dots')
  180.            
  181.         cv.ANGLEX = cv.ym
  182.         cv.ANGLEY = cv.xm
  183.        
  184.         canvas.create_text((30,20),text='Drag Mouse To Rotate 3D Object',anchor='w')
  185.         canvas.create_text((30,40),text='X: %d'%(cv.xm),anchor='w')
  186.         canvas.create_text((30,60),text='Y: %d'%(cv.ym),anchor='w')
  187.        
  188.         break
  189.     root.after(60, animate)
  190.  
  191. animate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement