Advertisement
here2share

# Tk_fibonacci_sphere.py

May 22nd, 2019
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.02 KB | None | 0 0
  1. # Tk_fibonacci_sphere.py
  2.  
  3. from Tkinter import *
  4. import math
  5. import random
  6. import time
  7.  
  8. root = Tk()
  9.  
  10. c_width = 600
  11. c_height = 600
  12. canvas = Canvas(root, width=c_width, height=c_height, bg='white')
  13. canvas.pack()
  14.  
  15. def fibonacci_sphere(samples=1):
  16.     points = []
  17.     offset = 2./samples
  18.     increment = math.pi * (3. - math.sqrt(5.));
  19.  
  20.     for i in range(samples):
  21.         y = ((i * offset) - 1) + (offset / 2);
  22.         r = math.sqrt(1 - pow(y,2))
  23.  
  24.         phi = ((i + 1.) % samples) * increment
  25.  
  26.         x = math.cos(phi) * r
  27.         z = math.sin(phi) * r
  28.  
  29.         points.append([x,y,z])
  30.     points.sort(key=lambda z:z[-1])
  31.     return points
  32.  
  33. wait = 0
  34. while 1:
  35.     if not wait:
  36.         points = random.randint(200,500)
  37.         for x,y,z in fibonacci_sphere(points):
  38.             x,y = [int(r*250+300) for r in [x,y]]
  39.             z = int((z+1)*50)/20
  40.             color = '#%02x%02x%02x' % (180, 210-z*12, 180)
  41.             canvas.create_oval(x, y, x+z+4, y+z+4, fill=color, outline=color)
  42.             wait = time.time()+2
  43.     if wait < time.time():
  44.         canvas.update()
  45.         canvas.delete('all')
  46.         wait = 0
  47.  
  48. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement