Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # dot packing animation
- import cv2
- import numpy as np
- from easing_functions import CubicEaseInOut
- # force window to 100% scale (windows only)
- import ctypes
- ctypes.windll.user32.SetProcessDPIAware()
- ints = lambda *args: tuple(map(int, args))
- rounds = lambda *args: tuple(map(round, args))
- class Dot:
- def __init__(self, xs, ys, size, ease=CubicEaseInOut()):
- self.xs = xs
- self.ys = ys
- self.size = size
- self.ease = ease
- def draw(self, img, t):
- i = int(t)
- f = t % 1
- max_i = min(len(self.xs), len(self.ys)) - 2
- if i > max_i:
- i = max_i
- f = 1.0
- f = self.ease(f)
- x = self.xs[i]*(1.0-f) + self.xs[i+1]*f
- y = self.ys[i]*(1.0-f) + self.ys[i+1]*f
- img = cv2.circle(img, rounds(x, y), self.size, 255, -1)
- # set parameters here
- count = 500
- spacing = 10
- img_x, img_y = 800, 800
- x0, y0 = spacing*2, img_y-spacing*2
- dx, dy = spacing, -spacing
- x_count_start, x_count_end = 1, int(count/4)
- # make dots
- dots = []
- for i in range(count):
- dot = Dot([], [], int(spacing*0.8/2))
- dots.append(dot)
- # calculate and save values
- for x_count in range(x_count_start, x_count_end+1):
- for i in range(count):
- ix = i % x_count
- iy = int(i / x_count)
- x = x0 + ix * dx
- y = y0 + iy * dy
- dots[i].xs.append(x)
- dots[i].ys.append(y)
- # animate
- t = 0.0
- while True:
- img = np.zeros((img_y, img_x), dtype='uint8')
- for i in range(count):
- dots[i].draw(img, t)
- cv2.imshow('img', img)
- key = cv2.waitKey(10)
- if key == ord('r'):
- t = 0.0
- elif key == ord('x'):
- cv2.destroyAllWindows()
- break
- t += 0.01
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement