Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Animated_Swirling.py
- import tkinter as tk
- from PIL import Image, ImageDraw, ImageTk
- import math
- import random
- def create_flat_image():
- line_spacing = 100
- num_lines_x = (ww - 20 - 6) // line_spacing
- num_lines_y = (hh - 20 - 6) // line_spacing
- margin = (ww - (num_lines_x * line_spacing)) // 2
- for i in range(num_lines_x + 1):
- x = margin + i * line_spacing
- for j in range(margin, hh - margin + 6, 6): ### ZZZ range(margin, hh - margin (((+ 6))), 6)
- j = min(550, j)
- ttt = [(j, x), (j + 6, x), (j + 6, x + 6), (j, x + 6)]
- for x0, y0 in ttt:
- vectors[(x0, y0)] = (x0, y0)
- key_vectors[(j, x)] = ttt
- ttt = [(x, j), (x + 6, j), (x + 6, j + 6), (x, j + 6)]
- for x0, y0 in ttt:
- vectors[(x0, y0)] = (x0, y0)
- key_vectors[(x, j)] = ttt
- def apply_diffeomorphism():
- image = Image.new('RGB', (ww, hh), 'white')
- draw = ImageDraw.Draw(image)
- center_x, center_y = ww // 2, hh // 2
- pace = []
- for (i, j) in key_vectors:
- xy = []
- for (x0, y0) in key_vectors[(i, j)]:
- (x, y) = vectors[(x0, y0)]
- if (x0, y0) not in pace:
- pace += [(x0, y0)]
- dx = x - center_x
- dy = y - center_y
- distance = math.sqrt(dx**2 + dy**2)
- distance /= ww
- rotation_angle = math.radians(inner_speed * (distance - 0.6 - outer_speed))
- x = dx * math.cos(rotation_angle) - dy * math.sin(rotation_angle) + center_x
- y = dx * math.sin(rotation_angle) + dy * math.cos(rotation_angle) + center_y
- xy += [(x, y)]
- vectors[(x0, y0)] = (x, y)
- draw.polygon(xy, outline='black', fill='black')
- return image
- def inner(event=None):
- global inner_speed
- inner_speed = inner_speed_var.get()
- def outer(event=None):
- global outer_speed
- outer_speed = outer_speed_var.get()
- ww, hh = 600, 600
- key_vectors = {}
- vectors = {}
- create_flat_image()
- root = tk.Tk()
- root.title("Animated Swirling")
- root.geometry(f"{ww+200}x{hh}+10+10")
- main_frame = tk.Frame(root)
- main_frame.pack(side=tk.LEFT, fill=tk.BOTH)
- canvas = tk.Canvas(main_frame, width=ww, height=hh)
- canvas.pack()
- slider_frame = tk.Frame(root)
- slider_frame.pack(side=tk.RIGHT, fill=tk.BOTH)
- inner_speed_var = tk.DoubleVar(value=1)
- inner_speed_slider = tk.Scale(slider_frame, from_=50.0, to=-50.0,
- resolution=0.1, orient=tk.VERTICAL, label="Inner", variable=inner_speed_var, length=560, command=inner)
- inner_speed_slider.pack(side=tk.LEFT, pady=10)
- outer_speed_var = tk.DoubleVar(value=0.00001)
- outer_speed_slider = tk.Scale(slider_frame, from_=1.0, to=0,
- resolution=0.00001, orient=tk.VERTICAL, label="Outer", variable=outer_speed_var, length=560, command=outer)
- outer_speed_slider.pack(side=tk.LEFT, pady=10)
- inner()
- outer()
- while True:
- image = apply_diffeomorphism()
- tk_image = ImageTk.PhotoImage(image)
- canvas.create_image(0, 0, anchor=tk.NW, image=tk_image)
- canvas.update()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement