Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.24 KB | None | 0 0
  1. from manimlib.imports import *
  2.  
  3. class MathMagic:
  4.     def __init__(self, array):
  5.         self.array_input = array
  6.         self.array_output = self.array_input.copy()
  7.         self.pixels = len(self.array_input)
  8.  
  9.     def get_array(self):
  10.         return self.array_output
  11.  
  12.     @staticmethod
  13.     def linear_step_func(x, x0, x1):
  14.         y= np.piecewise(x, [
  15.         x < x0,
  16.         (x >= x0) & (x <= x1),
  17.         x > x1],
  18.             [0.,
  19.             lambda x: x/(x1-x0)+x0/(x0-x1),
  20.              1.]
  21.         )
  22.         return y
  23.  
  24.     def p3(self, step): #creates a function that returns 0.5, .. 0.5 ,0.6...1
  25.         val= self.linear_step_func(step,0,1)
  26.         return val
  27.  
  28.     def p2(self, step): #creates a function that returns 0.5, .. 0.5 ,0.6...1
  29.         val= self.linear_step_func(step,0.5,1)
  30.         return val
  31.  
  32.     def p1(self, step): #creates a function that returns 0,0.1,0.2...0.5
  33.         val= self.linear_step_func(step,0,0.5)
  34.         return val
  35.  
  36.     def make_mask(self,pixel,step): # step goes from 0 to 1
  37.         mask= np.full(pixel,self.p2(step))
  38.         mask[1,1]=self.p1(step)
  39.         mask[4,4]=self.p3(step)
  40.         return mask
  41.  
  42.     def apply_mask(self,pixel,t):
  43.         mask= self.make_mask(pixel,t)
  44.         self.array_output=  self.array_input*mask
  45.  
  46. class ControlSubmobjects(Scene):
  47.     def construct(self):
  48.         PIXEL = 5
  49.         sq_array= VGroup()
  50.         for x in range(PIXEL):
  51.             for y in range(PIXEL):
  52.                 dot = Dot(point=(x, y, 0),side_length=0.1)
  53.                 sq_array.add(dot)
  54.         sq_array.move_to(ORIGIN)
  55.         self.add(sq_array)
  56.         my_tracker = ValueTracker(0)
  57.         end_val = 1
  58.         final_heights = np.ones((PIXEL,PIXEL))
  59.         a_math = MathMagic(final_heights)
  60.         def updater_func(mob):
  61.                 a_math.apply_mask((PIXEL,PIXEL),my_tracker.get_value())
  62.                 new_array=a_math.get_array()
  63.                 hght_vals= new_array.flatten()
  64.                 [el.set_height(i+0.01) for i,el in zip(hght_vals,sq_array.submobjects) ]
  65.                 return mob
  66.  
  67.         self.play(my_tracker.increment_value, end_val,
  68.                   UpdateFromFunc(sq_array, updater_func),
  69.                   rate_func=linear, run_time=2)
  70.         self.wait(2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement