Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_morph.py
- import tkinter as tk
- import math
- import random
- root = tk.Tk()
- root.title("tk Morph")
- root.geometry("%dx%d+0+0"%(600,600))
- canvas = tk.Canvas(root, bg='black', width=600, height=600)
- canvas.pack()
- radius = 320
- center_x, center_y = 300, 300
- number_of_points = 16
- morph_steps = 400
- points = []
- def plot():
- for i in range(number_of_points):
- angle = 2 * math.pi * i / number_of_points
- r = radius * random.uniform(0.2, 1)
- a = angle * random.uniform(0.95, 1)
- x = center_x + r * math.cos(a)
- y = center_y + r * math.sin(a)
- try:
- points[i] = (x, y)
- except:
- points.append((x, y))
- plot()
- prev_points = points[:]
- def animate():
- plot()
- steps = []
- for i in range(number_of_points):
- x1, y1 = points[i]
- x2, y2 = prev_points[i]
- steps += [((x1-x2)/morph_steps, (y1-y2)/morph_steps)]
- for step in range(morph_steps):
- morph = []
- for i in range(number_of_points):
- x1, y1 = prev_points[i]
- x2, y2 = steps[i]
- morph += [(x1 + x2 * step, y1 + y2 * step)]
- canvas.delete('all')
- canvas.create_polygon(morph, smooth=True, fill='blue')
- canvas.update()
- prev_points[:] = points
- canvas.after(1, animate)
- animate()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement