Advertisement
here2share

# Tk_spiral_sort_2.py

Feb 25th, 2022
1,305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.56 KB | None | 0 0
  1. # Tk_spiral_sort_2.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_2")
  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.    
  38. # set up spiral
  39. step = 1
  40. state = 0
  41. numSteps = 1
  42. turnCounter = 1
  43. stepSize = 5
  44. cols = ww / stepSize
  45. rows = hh / stepSize
  46. totalSteps = cols * rows
  47. x = ww / 2
  48. y = hh / 2
  49. xy = []
  50.  
  51. while 1:       
  52.     # Move according to state
  53.     x,y = switch(state)
  54.     xy.append((x,y))
  55.    
  56.     # Change state
  57.     if step % numSteps == 0:
  58.         state = (state + 1) % 4
  59.         turnCounter += 1
  60.         if turnCounter % 2 == 0:
  61.             numSteps += 1
  62.     step += 1  
  63.    
  64.     if step > totalSteps:
  65.         break
  66.  
  67. COLORS = 'red orange yellow green blue purple'.split()
  68. t = stepSize
  69. i = 100
  70. iii = 1
  71. s = ''
  72. while 1:
  73.     Lc = len(COLORS)
  74.     canvas.delete('all')
  75.     for x,y in xy:
  76.         if len(s) < 10:
  77.             s += str(i)
  78.             i += 1
  79.         iii = int(s[:3])
  80.         s = s[3:]
  81.         color = int(iii)%Lc
  82.         canvas.create_rectangle((x,y,x+t,y+t), fill=COLORS[color], outline='')
  83.     canvas.update()
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement