Advertisement
here2share

# tk_morph.py

May 12th, 2023
894
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.19 KB | None | 0 0
  1. # tk_morph.py
  2.  
  3. import tkinter as tk
  4. import math
  5. import random
  6.  
  7. root = tk.Tk()
  8. root.title("tk Morph")
  9. root.geometry("%dx%d+0+0"%(600,600))
  10. canvas = tk.Canvas(root, bg='black', width=600, height=600)
  11. canvas.pack()
  12.  
  13. radius = 320
  14. center_x, center_y = 300, 300
  15. number_of_points = 16
  16. morph_steps = 400
  17.  
  18. points = []
  19. def plot():
  20.     for i in range(number_of_points):
  21.         angle = 2 * math.pi * i / number_of_points
  22.         r = radius * random.uniform(0.2, 1)
  23.         a = angle * random.uniform(0.95, 1)
  24.         x = center_x + r * math.cos(a)
  25.         y = center_y + r * math.sin(a)
  26.         try:
  27.             points[i] = (x, y)
  28.         except:
  29.             points.append((x, y))
  30.            
  31. plot()
  32. prev_points = points[:]
  33.  
  34. def animate():
  35.     plot()
  36.     steps = []
  37.     for i in range(number_of_points):
  38.         x1, y1 = points[i]
  39.         x2, y2 = prev_points[i]
  40.         steps += [((x1-x2)/morph_steps, (y1-y2)/morph_steps)]
  41.     for step in range(morph_steps):
  42.         morph = []
  43.         for i in range(number_of_points):
  44.             x1, y1 = prev_points[i]
  45.             x2, y2 = steps[i]
  46.             morph += [(x1 + x2 * step, y1 + y2 * step)]
  47.         canvas.delete('all')
  48.         canvas.create_polygon(morph, smooth=True, fill='blue')
  49.         canvas.update()
  50.     prev_points[:] = points
  51.     canvas.after(1, animate)
  52.  
  53. animate()
  54. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement