Advertisement
yclee126

dot packing animation

Apr 26th, 2022
1,068
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | None | 0 0
  1. # dot packing animation
  2.  
  3. import cv2
  4. import numpy as np
  5. from easing_functions import CubicEaseInOut
  6.  
  7. # force window to 100% scale (windows only)
  8. import ctypes
  9. ctypes.windll.user32.SetProcessDPIAware()
  10.  
  11.  
  12. ints = lambda *args: tuple(map(int, args))
  13. rounds = lambda *args: tuple(map(round, args))
  14.  
  15. class Dot:
  16.     def __init__(self, xs, ys, size, ease=CubicEaseInOut()):
  17.         self.xs = xs
  18.         self.ys = ys
  19.         self.size = size
  20.         self.ease = ease
  21.    
  22.     def draw(self, img, t):
  23.         i = int(t)
  24.         f = t % 1
  25.  
  26.         max_i = min(len(self.xs), len(self.ys)) - 2
  27.         if i > max_i:
  28.             i = max_i
  29.             f = 1.0
  30.  
  31.         f = self.ease(f)
  32.  
  33.         x = self.xs[i]*(1.0-f) + self.xs[i+1]*f
  34.         y = self.ys[i]*(1.0-f) + self.ys[i+1]*f
  35.  
  36.         img = cv2.circle(img, rounds(x, y), self.size, 255, -1)
  37.  
  38.  
  39. # set parameters here
  40. count = 500
  41. spacing = 10
  42. img_x, img_y = 800, 800
  43.  
  44. x0, y0 = spacing*2, img_y-spacing*2
  45. dx, dy = spacing, -spacing
  46. x_count_start, x_count_end = 1, int(count/4)
  47.  
  48. # make dots
  49. dots = []
  50. for i in range(count):
  51.     dot = Dot([], [], int(spacing*0.8/2))
  52.     dots.append(dot)
  53.  
  54. # calculate and save values
  55. for x_count in range(x_count_start, x_count_end+1):
  56.     for i in range(count):
  57.         ix = i % x_count
  58.         iy = int(i / x_count)
  59.  
  60.         x = x0 + ix * dx
  61.         y = y0 + iy * dy
  62.  
  63.         dots[i].xs.append(x)
  64.         dots[i].ys.append(y)
  65.  
  66. # animate
  67. t = 0.0
  68. while True:
  69.     img = np.zeros((img_y, img_x), dtype='uint8')
  70.     for i in range(count):
  71.         dots[i].draw(img, t)
  72.    
  73.     cv2.imshow('img', img)
  74.     key = cv2.waitKey(10)
  75.  
  76.     if key == ord('r'):
  77.         t = 0.0
  78.     elif key == ord('x'):
  79.         cv2.destroyAllWindows()
  80.         break
  81.    
  82.     t += 0.01
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement