Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_donut.py
- from Tkinter import *
- from PIL import Image, ImageTk
- import math
- def oRGB(rgb):
- r,g,b = rgb
- return "#%02x%02x%02x" % (r,g,b)
- gradient=[]
- def z(r,g,b):
- gradient.append(oRGB([r,g,b]))
- r,g,b=255,0,0
- for g in range(256):
- z(r,g,b)
- for r in range(254, -1, -1):
- z(r,g,b)
- for b in range(256):
- z(r,g,b)
- for g in range(254, -1, -1):
- z(r,g,b)
- L = len(gradient)/2
- gradient = gradient[::L/12]
- gradient = gradient[1:-1]+gradient[::-1]
- max_rgb=len(gradient)
- sq = 680
- radius = int(sq*0.999)
- root = Tk()
- root.title("Tk_donut")
- root.geometry("%dx%d+0+0"%(sq,sq))
- canvas = Canvas(root, width=sq, height=sq)
- canvas.grid()
- img = Image.new("RGB",(sq, sq))
- white = (255, 255, 255)
- black = (0, 0, 0)
- hue = 0
- x_start, y_start = 0, 0
- x_separator = 10
- y_separator = 20
- rows = sq // y_separator
- columns = sq // x_separator
- screen_size = rows * columns
- x_offset = columns / 2
- y_offset = rows / 2
- A, B = 0, 0 # rotating animation
- theta_spacing = 10
- phi_spacing = 1 # for faster rotation change to 2, 3 or more, but first change 86, 87 lines as commented
- def display(color, x_start, y_start):
- xy = (x_start, y_start, x_start+10, y_start+10)
- canvas.create_rectangle(xy, fill=color, width=0)
- run = True
- while run:
- canvas.delete('all')
- z = [0] * screen_size # Donut. Fills donut space
- b = [' '] * screen_size # Background. Fills empty space
- for j in range(0, radius, theta_spacing): # from 0 to 2pi
- for i in range(0, radius, phi_spacing): # from 0 to 2pi
- c = math.sin(i)
- d = math.cos(j)
- e = math.sin(A)
- f = math.sin(j)
- g = math.cos(A)
- h = d + 2
- D = 1 / (c * h * e + f * g + 5)
- l = math.cos(i)
- m = math.cos(B)
- n = math.sin(B)
- t = c * h * g - f * e
- x = int(x_offset + 40 * D * (l * h * m - t * n)) # 3D x coordinate after rotation
- y = int(y_offset + 20 * D * (l * h * n + t * m)) # 3D y coordinate after rotation
- o = int(x + columns * y) # 3D z coordinate after rotation
- N = int(max_rgb * ((f * e - c * d * g) * m - c * d * e - f * g - l * d * n)) # luminance index
- if rows > y and y > 0 and x > 0 and columns > x and D > z[o]:
- z[o] = D
- b[o] = gradient[N%max_rgb]
- if y_start == rows * y_separator - y_separator:
- y_start = 0
- for i in range(len(b)):
- A += 0.00007
- B += 0.0001
- if not (i == 0 or i % columns):
- y_start += y_separator
- x_start = 0
- if b[i][0] == '#':
- display(b[i], x_start, y_start)
- x_start += x_separator
- hue += 1000
- canvas.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement