Advertisement
here2share

# Tk_spiral_of_9_integers.py

Feb 21st, 2022
841
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.55 KB | None | 0 0
  1. # Tk_spiral_of_9_integers.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_of_9_integers")
  18. root.geometry("%dx%d+0+0"%(ww,hh))
  19. canvas = Canvas(root, width=ww, height=hh)
  20. canvas.grid()
  21.  
  22. def isOf3(num):
  23.     for a in Random3:
  24.         if not num%a:
  25.             return False
  26.     return num > 3
  27.    
  28. def switch(state):
  29.     return {
  30.         0 : (x + stepSize, y),
  31.         1 : (x, y - stepSize),
  32.         2 : (x - stepSize, y),
  33.         3 : (x, y + stepSize),
  34.     }[state]
  35.    
  36. forRandom3 = [z for z in range(7,2000)]
  37.  
  38. while 1:
  39.     rs(forRandom3)
  40.     Random3 = forRandom3[:9]
  41.     print Random3
  42.    
  43.     for L in range(7,18):
  44.         # set up spiral
  45.         step = 1
  46.         state = 0
  47.         numSteps = 1
  48.         turnCounter = 1
  49.         stepSize = 5
  50.         cols = ww / stepSize
  51.         rows = hh / stepSize
  52.         totalSteps = cols * rows
  53.         x = ww / 2
  54.         y = hh / 2
  55.        
  56.         try: canvas.delete('all')
  57.         except: 0
  58.        
  59.         while 1:
  60.             # If prime... draw
  61.             if isOf3(step):
  62.                 color = (color + 1) % L
  63.             t = stepSize * 0.5
  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)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement