here2share

# Tk_radial_prime_in_color.py

Feb 22nd, 2022 (edited)
824
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. # Tk_radial_prime_in_color.py
  2.  
  3. from Tkinter import *
  4. from math import sqrt, atan2
  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. Lc = len(COLORS)
  14.  
  15. ww = 600
  16. hh = 600
  17. root = Tk()
  18. root.title("# Tk_radial_prime_in_color")
  19. root.geometry("%dx%d+0+0"%(ww,hh))
  20. canvas = Canvas(root, width=ww, height=hh)
  21. canvas.grid()
  22.  
  23. def isPrime(num):
  24.     a=2
  25.     while a <= sqrt(num):
  26.         if num%a < 1:
  27.             return a%Lc
  28.         a=a+1
  29.     return 0
  30.  
  31. # to record spiral
  32. stepSize = 5
  33. cols = ww / stepSize
  34. rows = hh / stepSize
  35. x = x0 = ww / 2
  36. y = y0 = hh / 2
  37. xy = []
  38. for y in range(0,hh,stepSize):
  39.     for x in range(0,ww,stepSize):
  40.         distance = sqrt((x0-x)**2+(y0-y)**2)
  41.         xy2 = atan2(x-x0,y-y0)
  42.         xy.append(((int(distance), xy2),x,y))
  43.        
  44. xy.sort()
  45. xy = [(x,y) for z,x,y in xy]
  46.  
  47. step = 1
  48. for x,y in xy:
  49.     color = isPrime(step)
  50.     t = stepSize
  51.     canvas.create_rectangle((x,y,x+t,y+t), fill=COLORS[color], outline='')
  52.     step += 1
  53.     COLORS = COLORS + [COLORS.pop(0)]
  54. canvas.update()
Add Comment
Please, Sign In to add comment