Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_3DsphereXspin.py ZZZ + auto_mesh
- from math import cos,sin,pi,radians,sqrt
- import random
- r=random.randint
- try:
- import tkinter as tk
- except:
- import Tkinter as tk
- root=tk.Tk()
- root.title("Tk 3D Sphere Spin")
- ww=640
- hh=640
- xc=ww/2
- yc=hh/2
- halfResX = ww/2
- halfResY = hh/2
- canvas=tk.Canvas(root,width=ww,height=hh)
- canvas.pack(fill="both",expand=True)
- COLORS='red orange yellow green lightblue purple'.split()
- class Cv():
- x,y,z=0,0,0
- xx,yy=0,0
- ANGLEX = 0
- ANGLEY = 0
- ANGLEZ = 0
- PZ = 200
- xm,ym,x2,y2 = 3,2,0,0
- cv=Cv()
- def click(event):
- cv.xx,cv.yy = (event.x,event.y)
- def drag(event):
- tx = (event.x-cv.xx)
- ty = (event.y-cv.yy)
- cv.xm += tx
- cv.ym += ty
- cv.xm %= 360
- cv.ym %= 360
- cv.xx,cv.yy = (event.x,event.y)
- canvas.bind('<Button-1>',click)
- canvas.bind('<B1-Motion>',drag)
- distance = 400
- x1, y1, x2, y2=200, 0, 400, 500
- angle=0
- speed=10
- x=y=z=10
- sides=7
- XYZ = {}
- def fibonacci_sphere(points=4):
- POINTS = []
- C = 0
- phi = pi*(3.-5.**.5) # golden angle in radians
- for i in range(points):
- y = 1-(i/(points - 1.))*2 # y goes from 1 to -1
- radius = sqrt(1-y*y) # radius at y
- theta = phi*i # golden angle increment
- x = cos(theta)*radius
- z = sin(theta)*radius
- POINTS.append((x,y,z))
- XYZ[x,y,z] = C
- return POINTS
- points = fibonacci_sphere(100)
- points.pop(0)
- def fn_distance(objA, objB):
- x1, y1, z1 = objA
- x2, y2, z2 = objB
- d = ((x2 - x1)**2 + (y2 - y1)**2 + (z2 - z1)**2)**0.333333
- return d
- SIDES = []
- L = len(points) - 1
- for y in range(L):
- for x in range(y,min(L,y+16)):
- for z in range(x,min(L,x+16)):
- ttt = [fn_distance(points[a],points[b]) for a,b in [(x,y),(x,z),(y,z)]]
- ttt.sort()
- if all(ttt):
- t = ttt[0]/ttt[2]
- if t > 0.69 and ttt[2] < 0.64:
- t = tuple(sorted([points[x],points[y],points[z]]))
- if t not in SIDES:
- SIDES += [t]
- print len(SIDES)
- SIDES.sort()
- RGBs = []
- def rgb_to_hex(rgb):
- return '#%02x%02x%02x' % rgb
- def z():
- RGBs.append(rgb_to_hex((r,g,b)))
- r,g,b = 255,0,0
- for g in range(256):
- z()
- for r in range(254, -1, -1):
- z()
- for b in range(256):
- z()
- for g in range(254, -1, -1):
- z()
- for r in range(256):
- z()
- for b in range(254, -1, -1):
- z()
- Lc = len(RGBs)
- c = 0
- sideColor = {}
- for t in SIDES:
- sideColor[t] = RGBs[c%Lc]
- c += 30
- def animate(vradius=100):
- while 1:
- canvas.delete('all')
- rax = cv.ANGLEX*pi/180
- ray = cv.ANGLEY*pi/180
- sinx = sin(rax)
- cosx = cos(rax)
- siny = sin(ray)
- cosy = cos(ray)
- r1 = []
- r2 = []
- xy = []
- for side in SIDES:
- t = []
- zt = []
- color = sideColor[side]
- for vectors in side:
- VX,VY,VZ = [z*vradius for z in vectors]
- YX = VX*cosy - VZ*siny - VX
- YZ = VX*siny + VZ*cosy - VZ
- XY = VY*cosx - (VZ+YZ)*sinx - VY
- XZ = VY*sinx + (VZ+YZ)*cosx - (VZ+YZ)
- ZR = XZ+YZ
- z = (VZ+cv.PZ+ZR)/distance
- x = ((VX+YX)/z)+halfResX
- y = ((VY+XY)/z)+halfResY
- t.append([x,y])
- zt.append(z)
- xy.append([max(zt),t,color,(x,y)])
- xy.sort(reverse=1)
- dots = []
- for z,side,color,dot in xy:
- canvas.create_polygon(side,fill=color,tag='ball_A')
- dots += [dot]
- L = len(dots)
- for x,y in dots[int(L*0.7):]:
- canvas.create_oval(x,y,x+5,y+5,fill='black',tag='dots')
- cv.ANGLEX = cv.ym
- cv.ANGLEY = cv.xm
- canvas.create_text((30,20),text='Drag Mouse To Rotate 3D Object',anchor='w')
- canvas.create_text((30,40),text='X: %d'%(cv.xm),anchor='w')
- canvas.create_text((30,60),text='Y: %d'%(cv.ym),anchor='w')
- break
- root.after(60, animate)
- animate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement