Advertisement
here2share

# Tk_radial_rainbow_001_offset.py

Oct 16th, 2022
1,058
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.37 KB | None | 0 0
  1. # Tk_radial_rainbow_001_offset.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()
  12. color = 0
  13. Lc = len(COLORS)
  14.  
  15. ww = 600
  16. hh = 600
  17. root = Tk()
  18. root.title("# Tk_radial_rainbow_001_offset")
  19. root.geometry("%dx%d+0+0"%(ww,hh))
  20. canvas = Canvas(root, width=ww, height=hh)
  21. canvas.grid()
  22.  
  23. def switch(state):
  24.     return {
  25.         0 : (x + stepSize, y),
  26.         1 : (x, y - stepSize),
  27.         2 : (x - stepSize, y),
  28.         3 : (x, y + stepSize),
  29.     }[state]
  30.  
  31. # to record spiral
  32. xy = []
  33.  
  34. # set up spiral
  35. step = 1
  36. state = 0
  37. numSteps = 1
  38. turnCounter = 1
  39. stepSize = 3
  40. cols = ww / stepSize
  41. rows = hh / stepSize
  42. totalSteps = cols * rows
  43. x = x0 = ww / 2
  44. y = y0 = hh / 2
  45.  
  46. while 1:
  47.     distance = int(sqrt((x0-x)**2+(y0-y)**2)) + (step*0.00001)
  48.     xy.append((distance,x,y))
  49.    
  50.     # Move according to state
  51.  
  52.     # Change state
  53.     if step % numSteps == 0:
  54.         state = (state + 1) % 4
  55.         turnCounter += 1
  56.         if turnCounter % 2 == 0:
  57.             numSteps += 1
  58.     step += 1
  59.     x, y = switch(state)
  60.  
  61.     # Are we done?
  62.     if step > totalSteps:
  63.         break
  64.        
  65. xy.sort()
  66. xy = [(x,y) for z,x,y in xy]
  67.  
  68. for x,y in xy:
  69.     color = COLORS[int(step) % Lc]
  70.     t = stepSize
  71.     canvas.create_rectangle((x,y,x+t,y+t), fill=color, outline='')
  72.     step = (step + 1.001)
  73.     canvas.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement