Advertisement
here2share

# Tk_radial_sort.py

Feb 22nd, 2022
818
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.52 KB | None | 0 0
  1. # Tk_radial_sort.py
  2.  
  3. from Tkinter import *
  4. import math
  5. from random import shuffle as rs
  6.  
  7. def oRGB(rgb): # pass
  8.     r,g,b = rgb
  9.     return "#%02x%02x%02x" % (r,g,b)
  10.    
  11. COLORS = 'red orange yellow green blue purple'.split()*5
  12. color = 0
  13.  
  14. ww = 600
  15. hh = 600
  16. root = Tk()
  17. root.title("# Tk_radial_sort")
  18. root.geometry("%dx%d+0+0"%(ww,hh))
  19. canvas = Canvas(root, width=ww, height=hh)
  20. canvas.grid()
  21.  
  22. xy = []
  23. PI = math.pi
  24. ww0 = ww/2
  25. hh0 = hh/2
  26.  
  27. for r in range(1,500,5):
  28.     circumference = (r * 2 * PI) / ((r + 200.0) / 50)
  29.     if not circumference:
  30.         circumference = 1
  31.     for degrees in range(0,360,int(360.0/circumference)):
  32.         angle = math.radians(degrees)
  33.         x = int(r * math.cos(angle))+ww0
  34.         y = int(r * math.sin(angle))+hh0
  35.         if 0 < x < ww-6 and 0 < y < hh-6:
  36.             xy.append((x,y))
  37.             canvas.create_oval(x,y,x+8,y+8, fill="purple", outline="")
  38. canvas.update()
  39.  
  40. def isAlive(num):
  41.     for a in RandomAlive:
  42.         if not num%a:
  43.             return RandomAlive.index(a)
  44.     return (color + 7) % 12
  45.  
  46.  
  47. def switch(state):
  48.     return {
  49.         0 : (x + stepSize, y),
  50.         1 : (x, y - stepSize),
  51.         2 : (x - stepSize, y),
  52.         3 : (x, y + stepSize),
  53.     }[state]
  54.    
  55. forRandomAlive = [z for z in range(3,2000)]
  56.  
  57. while 1:
  58.     rs(forRandomAlive)
  59.     RandomAlive = forRandomAlive[:12]
  60.     RandomAlive.sort(reverse=1)
  61.     print RandomAlive
  62.    
  63.     # set up spiral
  64.     step = 1
  65.    
  66.     canvas.delete('all')
  67.    
  68.     for x,y in xy:
  69.         color = isAlive(step)
  70.         canvas.create_oval(x,y,x+8,y+8, fill=COLORS[color], outline="")
  71.        
  72.         step += 1  
  73.        
  74.     canvas.update()
  75.     rs(COLORS)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement