Advertisement
here2share

# Tk_donut.py

Mar 31st, 2021
1,116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.51 KB | None | 0 0
  1. # Tk_donut.py
  2.  
  3. from Tkinter import *
  4. from PIL import Image, ImageTk
  5. import math
  6.  
  7. def oRGB(rgb):
  8.     r,g,b = rgb
  9.     return "#%02x%02x%02x" % (r,g,b)
  10.    
  11. gradient=[]
  12. def z(r,g,b):
  13.     gradient.append(oRGB([r,g,b]))
  14. r,g,b=255,0,0
  15. for g in range(256):
  16.     z(r,g,b)
  17. for r in range(254, -1, -1):
  18.     z(r,g,b)
  19. for b in range(256):
  20.     z(r,g,b)
  21. for g in range(254, -1, -1):
  22.     z(r,g,b)
  23. L = len(gradient)/2
  24. gradient = gradient[::L/12]
  25. gradient = gradient[1:-1]+gradient[::-1]
  26. max_rgb=len(gradient)
  27.  
  28. sq = 680
  29. radius = int(sq*0.999)
  30. root = Tk()
  31. root.title("Tk_donut")
  32. root.geometry("%dx%d+0+0"%(sq,sq))
  33. canvas = Canvas(root, width=sq, height=sq)
  34. canvas.grid()
  35.  
  36. img = Image.new("RGB",(sq, sq))
  37.  
  38. white = (255, 255, 255)
  39. black = (0, 0, 0)
  40. hue = 0
  41.  
  42. x_start, y_start = 0, 0
  43.  
  44. x_separator = 10
  45. y_separator = 20
  46.  
  47. rows = sq // y_separator
  48. columns = sq // x_separator
  49. screen_size = rows * columns
  50.  
  51. x_offset = columns / 2
  52. y_offset = rows / 2
  53.  
  54. A, B = 0, 0  # rotating animation
  55.  
  56. theta_spacing = 10
  57. phi_spacing = 1 # for faster rotation change to 2, 3 or more, but first change 86, 87 lines as commented
  58.  
  59. def display(color, x_start, y_start):
  60.     xy = (x_start, y_start, x_start+10, y_start+10)
  61.     canvas.create_rectangle(xy, fill=color, width=0)
  62.  
  63. run = True
  64. while run:
  65.  
  66.     canvas.delete('all')
  67.  
  68.     z = [0] * screen_size  # Donut. Fills donut space
  69.     b = [' '] * screen_size  # Background. Fills empty space
  70.  
  71.     for j in range(0, radius, theta_spacing):  # from 0 to 2pi
  72.         for i in range(0, radius, phi_spacing):  # from 0 to 2pi
  73.             c = math.sin(i)
  74.             d = math.cos(j)
  75.             e = math.sin(A)
  76.             f = math.sin(j)
  77.             g = math.cos(A)
  78.             h = d + 2
  79.             D = 1 / (c * h * e + f * g + 5)
  80.             l = math.cos(i)
  81.             m = math.cos(B)
  82.             n = math.sin(B)
  83.             t = c * h * g - f * e
  84.             x = int(x_offset + 40 * D * (l * h * m - t * n))  # 3D x coordinate after rotation
  85.             y = int(y_offset + 20 * D * (l * h * n + t * m))  # 3D y coordinate after rotation
  86.             o = int(x + columns * y)  # 3D z coordinate after rotation
  87.             N = int(max_rgb * ((f * e - c * d * g) * m - c * d * e - f * g - l * d * n))  # luminance index
  88.             if rows > y and y > 0 and x > 0 and columns > x and D > z[o]:
  89.                 z[o] = D
  90.                 b[o] = gradient[N%max_rgb]
  91.  
  92.     if y_start == rows * y_separator - y_separator:
  93.         y_start = 0
  94.  
  95.     for i in range(len(b)):
  96.         A += 0.00007
  97.         B += 0.0001
  98.         if not (i == 0 or i % columns):
  99.             y_start += y_separator
  100.             x_start = 0
  101.         if b[i][0] == '#':
  102.             display(b[i], x_start, y_start)
  103.         x_start += x_separator
  104.  
  105.     hue += 1000
  106.     canvas.update()
  107.    
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement