Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_Square_Spiral_Fractal_Animation.py
- import tkinter as tk
- import math
- WIDTH, HEIGHT = 300, 300
- root = tk.Tk()
- root.title("Square Spiral Fractal Animation")
- canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT, bg='black')
- canvas.pack()
- rainbow=[]
- def z(r,g,b):
- rainbow.append(f'#{r:02x}{g:02x}{b:02x}')
- 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)
- for r in range(256):
- z(r,g,b)
- for b in range(254, -1, -1):
- z(r,g,b)
- Lcolors = len(rainbow) - 1
- steps = (WIDTH/5)*(HEIGHT/5)
- incr = 3 / steps
- div = 50 / steps
- def spiral_coords(width, height, step):
- cx, cy = width // 2, height // 2
- x, y = 0, 0
- dx, dy = 0, -1
- max_side = max(width, height)
- coords = []
- for i in range((max_side//step + 2) ** 2):
- px, py = cx + x*step, cy + y*step
- if 0 <= px < width and 0 <= py < height:
- coords.append((px, py))
- if x == y or (x < 0 and x == -y) or (x > 0 and x == 1-y):
- dx, dy = -dy, dx
- x, y = x+dx, y+dy
- return coords
- spiral_order = spiral_coords(WIDTH, HEIGHT, 5)[::-1]
- pixels = {}
- i = 0
- for x, y in spiral_order:
- j = 25 + ((i * incr) * (div * i))
- pixels[x, y] = [j, j]
- i += 1
- while 1:
- canvas.delete("all")
- for x, y in spiral_order:
- i, j = pixels[x, y]
- j = (i + j) % Lcolors
- pixels[x, y] = [i, j]
- color = rainbow[int(j)]
- canvas.create_rectangle(x, y, x+5, y+5, fill=color, outline='')
- canvas.update()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment