Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_radial_prime_in_color.py
- from Tkinter import *
- from math import sqrt
- from random import shuffle as rs
- def oRGB(rgb): # pass
- r,g,b = rgb
- return "#%02x%02x%02x" % (r,g,b)
- COLORS = 'red orange yellow green blue purple'.split()*5
- color = 0
- Lc = len(COLORS)
- ww = 600
- hh = 600
- root = Tk()
- root.title("# Tk_radial_prime_in_color")
- root.geometry("%dx%d+0+0"%(ww,hh))
- canvas = Canvas(root, width=ww, height=hh)
- canvas.grid()
- def isPrime(num):
- a=2
- while a <= sqrt(num):
- if num%a < 1:
- return a%Lc
- a=a+1
- return 0
- def switch(state):
- return {
- 0 : (x + stepSize, y),
- 1 : (x, y - stepSize),
- 2 : (x - stepSize, y),
- 3 : (x, y + stepSize),
- }[state]
- # to record spiral
- xy = []
- # set up spiral
- step = 1
- state = 0
- numSteps = 1
- turnCounter = 1
- stepSize = 3
- cols = ww / stepSize
- rows = hh / stepSize
- totalSteps = cols * rows
- x = x0 = ww / 2
- y = y0 = hh / 2
- while 1:
- distance = int(sqrt((x0-x)**2+(y0-y)**2)) + (step*0.00001)
- xy.append((distance,x,y))
- # Move according to state
- # Change state
- if step % numSteps == 0:
- state = (state + 1) % 4
- turnCounter += 1
- if turnCounter % 2 == 0:
- numSteps += 1
- step += 1
- x, y = switch(state)
- # Are we done?
- if step > totalSteps:
- break
- xy.sort()
- xy = [(x,y) for z,x,y in xy]
- for x,y in xy:
- color = isPrime(step)
- t = stepSize
- canvas.create_rectangle((x,y,x+t,y+t), fill=COLORS[color], outline='')
- step += 1
- canvas.update()
- COLORS = COLORS + [COLORS.pop(0)]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement