here2share

# Tk_spiral_sort.py

Feb 21st, 2022 (edited)
1,104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.52 KB | None | 0 0
  1. # Tk_spiral_sort.py
  2.  
  3. from Tkinter import *
  4. from math import sqrt
  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_spiral_sort")
  18. root.geometry("%dx%d+0+0"%(ww,hh))
  19. canvas = Canvas(root, width=ww, height=hh)
  20. canvas.grid()
  21.  
  22. def isAlive(num):
  23.     for a in RandomAlive:
  24.         if not num%a:
  25.             return RandomAlive.index(a)
  26.     return (color + 7) % 12
  27.  
  28.  
  29. def switch(state):
  30.     return {
  31.         0 : (x + stepSize, y),
  32.         1 : (x, y - stepSize),
  33.         2 : (x - stepSize, y),
  34.         3 : (x, y + stepSize),
  35.     }[state]
  36.    
  37. forRandomAlive = [z for z in range(3,2000)]
  38.  
  39. while 1:
  40.     rs(forRandomAlive)
  41.     RandomAlive = forRandomAlive[:12]
  42.     RandomAlive.sort(reverse=1)
  43.     print RandomAlive
  44.    
  45.     # set up spiral
  46.     step = 1
  47.     state = 0
  48.     numSteps = 1
  49.     turnCounter = 1
  50.     stepSize = 5
  51.     cols = ww / stepSize
  52.     rows = hh / stepSize
  53.     totalSteps = cols * rows
  54.     x = ww / 2
  55.     y = hh / 2
  56.    
  57.     try: canvas.delete('all')
  58.     except: 0
  59.    
  60.     while 1:
  61.         # If prime... draw
  62.         color = isAlive(step)
  63.         t = stepSize
  64.         canvas.create_rectangle((x,y,x+t,y+t), fill=COLORS[color], outline='')
  65.        
  66.         # Move according to state
  67.         x, y = switch(state)
  68.        
  69.         # Change state
  70.         if step % numSteps == 0:
  71.             state = (state + 1) % 4
  72.             turnCounter += 1
  73.             if turnCounter % 2 == 0:
  74.                 numSteps += 1
  75.         step += 1  
  76.        
  77.         # Are we done?
  78.         if step > totalSteps:
  79.             break
  80.     canvas.update()
  81.     rs(COLORS)
Add Comment
Please, Sign In to add comment