Advertisement
here2share

# Animated_Swirling.py

Jun 9th, 2024
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.83 KB | None | 0 0
  1. # Animated_Swirling.py
  2.  
  3. import tkinter as tk
  4. from PIL import Image, ImageDraw, ImageTk
  5. import math
  6. import random
  7.  
  8. def create_flat_image():
  9.     line_spacing = 100
  10.  
  11.     num_lines_x = (ww - 20 - 6) // line_spacing
  12.     num_lines_y = (hh - 20 - 6) // line_spacing
  13.     margin = (ww - (num_lines_x * line_spacing)) // 2
  14.  
  15.     for i in range(num_lines_x + 1):
  16.         x = margin + i * line_spacing
  17.         for j in range(margin, hh - margin + 6, 6): ### ZZZ range(margin, hh - margin (((+ 6))), 6)
  18.             j = min(550, j)
  19.             ttt = [(j, x), (j + 6, x), (j + 6, x + 6), (j, x + 6)]
  20.             for x0, y0 in ttt:
  21.                 vectors[(x0, y0)] = (x0, y0)
  22.             key_vectors[(j, x)] = ttt
  23.             ttt = [(x, j), (x + 6, j), (x + 6, j + 6), (x, j + 6)]
  24.             for x0, y0 in ttt:
  25.                 vectors[(x0, y0)] = (x0, y0)
  26.             key_vectors[(x, j)] = ttt
  27.  
  28. def apply_diffeomorphism():
  29.     image = Image.new('RGB', (ww, hh), 'white')
  30.     draw = ImageDraw.Draw(image)
  31.     center_x, center_y = ww // 2, hh // 2
  32.     pace = []
  33.  
  34.     for (i, j) in key_vectors:
  35.         xy = []
  36.         for (x0, y0) in key_vectors[(i, j)]:
  37.             (x, y) = vectors[(x0, y0)]
  38.             if (x0, y0) not in pace:
  39.                 pace += [(x0, y0)]
  40.                 dx = x - center_x
  41.                 dy = y - center_y
  42.                 distance = math.sqrt(dx**2 + dy**2)
  43.                 distance /= ww
  44.  
  45.                 rotation_angle = math.radians(inner_speed * (distance - 0.6 - outer_speed))
  46.  
  47.                 x = dx * math.cos(rotation_angle) - dy * math.sin(rotation_angle) + center_x
  48.                 y = dx * math.sin(rotation_angle) + dy * math.cos(rotation_angle) + center_y
  49.  
  50.             xy += [(x, y)]
  51.             vectors[(x0, y0)] = (x, y)
  52.  
  53.         draw.polygon(xy, outline='black', fill='black')
  54.  
  55.     return image
  56.  
  57.  
  58. def inner(event=None):
  59.     global inner_speed
  60.     inner_speed = inner_speed_var.get()
  61.  
  62. def outer(event=None):
  63.     global outer_speed
  64.     outer_speed = outer_speed_var.get()
  65.  
  66. ww, hh = 600, 600
  67. key_vectors = {}
  68. vectors = {}
  69. create_flat_image()
  70.  
  71. root = tk.Tk()
  72. root.title("Animated Swirling")
  73. root.geometry(f"{ww+200}x{hh}+10+10")
  74.  
  75. main_frame = tk.Frame(root)
  76. main_frame.pack(side=tk.LEFT, fill=tk.BOTH)
  77. canvas = tk.Canvas(main_frame, width=ww, height=hh)
  78. canvas.pack()
  79.  
  80. slider_frame = tk.Frame(root)
  81. slider_frame.pack(side=tk.RIGHT, fill=tk.BOTH)
  82.  
  83. inner_speed_var = tk.DoubleVar(value=1)
  84. inner_speed_slider = tk.Scale(slider_frame, from_=50.0, to=-50.0,
  85.         resolution=0.1, orient=tk.VERTICAL, label="Inner", variable=inner_speed_var, length=560, command=inner)
  86. inner_speed_slider.pack(side=tk.LEFT, pady=10)
  87.  
  88. outer_speed_var = tk.DoubleVar(value=0.00001)
  89. outer_speed_slider = tk.Scale(slider_frame, from_=1.0, to=0,
  90.         resolution=0.00001, orient=tk.VERTICAL, label="Outer", variable=outer_speed_var, length=560, command=outer)
  91. outer_speed_slider.pack(side=tk.LEFT, pady=10)
  92.  
  93. inner()
  94. outer()
  95.  
  96. while True:
  97.     image = apply_diffeomorphism()
  98.     tk_image = ImageTk.PhotoImage(image)
  99.     canvas.create_image(0, 0, anchor=tk.NW, image=tk_image)
  100.     canvas.update()
  101.  
  102. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement