strigido

Day 13 equation animation with Manim

Dec 31st, 2024
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.47 KB | Software | 0 0
  1. from manim import *
  2. from MF_Tools import *
  3.  
  4.  
  5. # LIGHT THEME
  6.  
  7. config.background_color = WHITE
  8.  
  9. # Those are objects which are WHITE by default
  10. # Define the wanted color for each one
  11. white_objects ={
  12.     Angle: ('dot_color', BLACK),
  13.     AnnotationDot: ('stroke_color', BLACK),
  14.     AnnularSector: ('color', BLACK),
  15.     Annulus: ('color', BLACK),
  16.     Arrow: ('color', BLACK),
  17.     Arrow3D: ('color', BLACK),
  18.     ArrowVectorField: ('color', BLACK),
  19.     Code: ('background_stroke_color', BLACK),
  20.     CubicBezier: ('color', BLACK),
  21.     DashedVMobject: ('color', BLACK),
  22.     Dot: ('color', BLACK),
  23.     Dot3D: ('color', BLACK),
  24.     Line: ('color', BLACK),
  25.     Line3D: ('color', BLACK),
  26.     MarkupText: ('color', BLACK),
  27.     Polygon: ('color', BLACK),
  28.     Rectangle: ('color', BLACK),
  29.     SingleStringMathTex: ('color', BLACK),
  30.     StreamLines: ('color', BLACK),
  31.     Text: ('color', BLACK),
  32.     TracedPath: ('stroke_color', BLACK),
  33.     VectorField: ('color', BLACK),
  34. }
  35.  
  36. for obj, (attr, color) in white_objects.items():
  37.     obj.set_default(**{attr: color})
  38.  
  39. # Other configurations
  40. Table.set_default(line_config=
  41.     {"stroke_width": 1, "stroke_opacity": 0.5, "color": BLACK})
  42.  
  43. Code.set_default(style="pastie")
  44. YELLOW="#ebe534"
  45.  
  46.  
  47.  
  48.  
  49.  
  50. # ANIMATION
  51.  
  52. class EquationAnimation(Scene):
  53.     def construct(self):
  54.         A_COLOR = PURE_GREEN
  55.         B_COLOR = PURE_BLUE
  56.         SCALE = 1.6
  57.         WAIT_TIME = 0.8
  58.         SPACING = 1
  59.        
  60.         eq1 = MathTex('x_P = ax_A + bx_B').scale(SCALE)
  61.         eq2 = MathTex('y_P = ay_A + by_B').scale(SCALE)
  62.         original_eqs = Group(eq1, eq2)
  63.  
  64.         eq2.next_to(eq1, DOWN, SPACING)
  65.         original_eqs.move_to(ORIGIN)
  66.  
  67.         self.wait(WAIT_TIME)
  68.         self.play(Write(eq1), Write(eq2))
  69.         self.play(eq1[0][3].animate.set_color(A_COLOR), eq1[0][7].animate.set_color(B_COLOR), eq2[0][3].animate.set_color(A_COLOR), eq2[0][7].animate.set_color(B_COLOR))
  70.         self.wait(WAIT_TIME)
  71.        
  72.         eq_b1 = MathTex('y_P - ay_A = by_B').scale(SCALE)
  73.         eq_b1.next_to(eq1, DOWN, SPACING)
  74.         eq_b1[0][3].set_color(A_COLOR)
  75.         eq_b1[0][7].set_color(B_COLOR)
  76.         self.play(TransformByGlyphMap(eq2, eq_b1,
  77.             ([2],[6], {"path_arc": 0.9*PI}),
  78.             ([6],[2], {"path_arc": -0.9*PI}),
  79.         ))
  80.         self.wait(WAIT_TIME)
  81.        
  82.         eq_b2 = MathTex(r'\frac{y_P-ay_A}{y_B}=b').scale(SCALE)
  83.         eq_b2.next_to(eq1, DOWN, SPACING)
  84.         eq_b2[0][3].set_color(A_COLOR)
  85.         eq_b2[0][10].set_color(B_COLOR)
  86.         self.play(TransformByGlyphMap(eq_b1, eq_b2,
  87.             ([8,9],[7,8], {"path_arc": -0.4*PI}),
  88.             ([], [6], {'shift': UP}),
  89.         ))
  90.         self.wait(WAIT_TIME)
  91.        
  92.         eq_b = MathTex(r'b = \frac{y_P-ay_A}{y_B}').scale(SCALE)
  93.         eq_b.next_to(eq1, DOWN, SPACING)
  94.         eq_b[0][5].set_color(A_COLOR)
  95.         eq_b[0][0].set_color(B_COLOR)
  96.         self.play(TransformByGlyphMap(eq_b2, eq_b,
  97.             ([10,9],[0,1], {"path_arc": -0.9*PI}),
  98.         ))
  99.         self.wait(WAIT_TIME)
  100.        
  101.         self.play(eq_b.animate.set_color(B_COLOR))
  102.         self.wait(WAIT_TIME)
  103.        
  104.         eq_a1 = MathTex(r'x_P = ax_A + \Big( \frac{y_P-ay_A}{y_B} \Big) x_B').scale(SCALE)
  105.         eq_a1.next_to(eq_b, UP, SPACING)
  106.         eq_a1[0][3].set_color(A_COLOR)
  107.         eq_a1[0][8:17].set_color(B_COLOR)
  108.         self.play(TransformByGlyphMap(eq1, eq_a1,
  109.             ([7],range(8,17)),
  110.             ([],[7], {'shift': LEFT, 'delay': 0.16}),
  111.             ([],[17], {'shift': RIGHT, 'delay': 0.16}),
  112.         ))
  113.         self.wait(WAIT_TIME)
  114.        
  115.         self.play(eq_a1[0][8:11].animate.set_color(BLACK), eq_a1[0][12:17].animate.set_color(BLACK), eq_a1[0][11].animate.set_color(A_COLOR))
  116.         self.wait(WAIT_TIME)
  117.        
  118.         eq_a2 = MathTex(r'x_P = ax_A + \frac{(y_P - ay_A) x_B}{y_B}').scale(SCALE)
  119.         eq_a2.next_to(eq_b, UP, SPACING)
  120.         eq_a2[0][3].set_color(A_COLOR)
  121.         eq_a2[0][11].set_color(A_COLOR)
  122.         self.play(TransformByGlyphMap(eq_a1, eq_a2,
  123.             ([7],[7]),      # (
  124.             ([17],[14]),    # )
  125.             ([14],[17]),    # \over
  126.             ([18,19],[15,16]),
  127.             ([15,16],[18,19]),
  128.         ))
  129.         self.wait(WAIT_TIME)
  130.        
  131.         eq_a3 = MathTex(r'x_P = \frac{ax_Ay_B}{y_B} + \frac{(y_P-ay_A)x_B}{y_B}').scale(SCALE)
  132.         eq_a3.next_to(eq_b, UP, SPACING)
  133.         eq_a3[0][3].set_color(A_COLOR)
  134.         eq_a3[0][16].set_color(A_COLOR)
  135.         self.play(TransformByGlyphMap(eq_a2, eq_a3,
  136.             ([],[6,7], {'shift': DOWN}),
  137.             ([],[8], {'shift': UP}),
  138.             ([],[9,10], {'shift': UP}),
  139.         ))
  140.         self.wait(WAIT_TIME)
  141.        
  142.         eq_a4 = MathTex('x_P y_B = ax_A y_B + (y_P - ay_A) x_B').scale(SCALE)
  143.         eq_a4.next_to(eq_b, UP, SPACING)
  144.         eq_a4[0][5].set_color(A_COLOR)
  145.         eq_a4[0][15].set_color(A_COLOR)
  146.         self.play(TransformByGlyphMap(eq_a3, eq_a4,
  147.             ([8,22],[], {'shift': DOWN}),
  148.             ([9,10],[2,3], {'path_arc': -0.5*PI}),
  149.             ([23,24],[2,3], {'path_arc': -0.26*PI}),
  150.             ([0,1],[0,1]),
  151.             (range(2,8),range(4,10)),
  152.             (range(11,22),range(10,21)),
  153.         ))
  154.         self.wait(WAIT_TIME)
  155.        
  156.         eq_a5 = MathTex('x_P y_B = ax_A y_B + y_P x_B - ay_A x_B').scale(SCALE)
  157.         eq_a5.next_to(eq_b, UP, SPACING)
  158.         eq_a5[0][5].set_color(A_COLOR)
  159.         eq_a5[0][16].set_color(A_COLOR)
  160.         self.play(TransformByGlyphMap(eq_a4, eq_a5,
  161.             ([11], ShrinkToCenter),
  162.             ([18], ShrinkToCenter),
  163.             ([12,13],[11,12]),
  164.             (range(14,18),range(15,19)),
  165.             ([19,20],[19,20]),
  166.             ([19,20],[13,14], {'path_arc': 0.8*PI}),
  167.         ))
  168.         self.wait(WAIT_TIME)
  169.        
  170.         eq_a6 = MathTex('x_P y_B - y_P x_B = ax_A y_B - ay_A x_B').scale(SCALE)
  171.         eq_a6.next_to(eq_b, UP, SPACING)
  172.         eq_a6[0][10].set_color(A_COLOR)
  173.         eq_a6[0][16].set_color(A_COLOR)
  174.         self.play(TransformByGlyphMap(eq_a5, eq_a6,
  175.             (range(10,15),range(4,9), {'path_arc': 0.84*PI}),
  176.         ))
  177.         self.wait(WAIT_TIME)
  178.        
  179.         eq_a7 = MathTex('x_P y_B - y_P x_B = a(x_A y_B - y_A x_B)').scale(SCALE)
  180.         eq_a7.next_to(eq_b, UP, SPACING)
  181.         eq_a7[0][10].set_color(A_COLOR)
  182.         self.play(TransformByGlyphMap(eq_a6, eq_a7,
  183.             ([16],[10], {'path_arc': 0.85*PI}),
  184.             ([10],[10]),
  185.             (GrowFromCenter,[11]),
  186.             (GrowFromCenter,[21]),
  187.             ([11,12,13,14,15,17,18,19,20], range(12,21)),
  188.         ))
  189.         self.wait(WAIT_TIME)
  190.        
  191.         eq_a8 = MathTex(r'\frac{x_P y_B - y_P x_B}{x_A y_B - y_A x_B} = a').scale(SCALE)
  192.         eq_a8.next_to(eq_b, UP, SPACING)
  193.         eq_a8[0][20].set_color(A_COLOR)
  194.         self.play(TransformByGlyphMap(eq_a7, eq_a8,
  195.             ([11], ShrinkToCenter),
  196.             ([21], ShrinkToCenter),
  197.             ([],[9], {'shift': UP}),
  198.             (range(12,21),range(10,19), {'path_arc': -0.52*PI}),
  199.         ))
  200.         self.wait(WAIT_TIME)
  201.        
  202.         eq_a = MathTex(r'a = \frac{x_P y_B - y_P x_B}{x_A y_B - y_A x_B}').scale(SCALE)
  203.         eq_a.next_to(eq_b, UP, SPACING)
  204.         eq_a[0][0].set_color(A_COLOR)
  205.         self.play(TransformByGlyphMap(eq_a8, eq_a,
  206.             ([20],[0], {'path_arc': 0.7*PI}),
  207.             ([19],[1], {'path_arc': 0.66*PI}),
  208.         ))
  209.         self.wait(WAIT_TIME)
  210.        
  211.         self.play(eq_a.animate.set_color(A_COLOR))
  212.         self.wait(WAIT_TIME)
  213.        
  214.         self.wait(2)
  215.  
  216.  
  217.  
Add Comment
Please, Sign In to add comment