main.py (5122B)
1 from manim import * 2 import numpy as np 3 class CreateSimpleExample(Scene): 4 def construct(self): 5 colorList = [RED, GREEN, BLUE, YELLOW] 6 7 # Create the grid 8 grid = Axes( 9 x_range=[0, 10, .5], 10 y_range=[0, 10, .5], 11 x_length=9, 12 y_length=5.5, 13 axis_config={ 14 "numbers_to_include": np.arange(0, 11, 1), 15 "font_size": 24, 16 }, 17 tips=False, 18 ) 19 20 y_label = grid.get_y_axis_label("y") 21 x_label = grid.get_x_axis_label("x") 22 23 self.play(Create(grid), Create(y_label), Create(x_label)) 24 25 points = [] # To store points and their target locations 26 animations = [] # To store the animations for moving points 27 28 for i in range(200): 29 target_blue = [(np.random.random() * 7.8) - 3.9, (0.5 * np.random.random() * 3.5) + .1, 0] 30 target_red = [(np.random.random() * 7.8) - 3.9, (0.5 * np.random.random() * -3.8) - .1, 0] 31 32 blue_point = Dot(point=[0, 0, 0], color=BLUE, radius=.05) 33 red_point = Dot(point=[0, 0, 0], color=RED, radius=.05) 34 35 self.add(blue_point, red_point) 36 37 # Store the points and their animations to move to target positions 38 animations.append(blue_point.animate.move_to(target_blue)) 39 animations.append(red_point.animate.move_to(target_red)) 40 41 # Play animations to move points to their target positions 42 self.play(*animations) 43 self.wait(duration=1) 44 45 46 text = Text("Impurity = 0.5").move_to([0,3,0]) 47 48 49 self.play(Create(text)) 50 51 self.wait(duration=1) 52 53 self.wait(1) 54 55 line = Line(start=[-4, 0,0], end=[4,0,0]) 56 self.play(Create(line)) 57 58 self.wait(2) 59 60 61 t1 = Rectangle(width=8, height=0).set_fill(BLUE, opacity=.2) 62 t2 = Rectangle(width=8).set_fill(BLUE, opacity=.3).move_to([0,1,0]) 63 64 m1 = Rectangle(width=8, height=0).set_fill(RED, opacity=.2) 65 m2 = Rectangle(width=8).set_fill(RED, opacity=.3).move_to([0,-1,0]) 66 self.play(Transform(m1,m2), Transform(t1,t2)) 67 68 self.wait(duration=1) 69 70 text3 = Text("Impurity = 0.0").move_to([0,3,0]) 71 self.play(Transform(text,text3)) 72 73 self.wait(duration=2) 74 75 76 77 class CreateMoreComplex(Scene): 78 def construct(self): 79 colorList = [RED, GREEN, BLUE, YELLOW] 80 81 # Create the grid 82 grid = Axes( 83 x_range=[0, 10, .5], 84 y_range=[0, 10, .5], 85 x_length=9, 86 y_length=5.5, 87 axis_config={ 88 "numbers_to_include": np.arange(0, 11, 1), 89 "font_size": 24, 90 }, 91 tips=False, 92 ) 93 94 y_label = grid.get_y_axis_label("y") 95 x_label = grid.get_x_axis_label("x") 96 97 self.play(Create(grid), Create(y_label), Create(x_label)) 98 99 points = [] # To store points and their target locations 100 animations = [] # To store the animations for moving points 101 102 for i in range(200): 103 target_blue = [(np.random.random() * 7.8) - 3.9, (0.5 * np.random.random() * 3.5) + .1, 0] 104 target_red = [(np.random.random() * (7.4 / 2)) - 3.9, (0.5 * np.random.random() * -3.5) - .1, 0] 105 target_yellow = [(np.random.random() * (7.4 / 2)), (0.5 * np.random.random() * -3.5) - .1, 0] 106 107 blue_point = Dot(point=[0, 0, 0], color=BLUE, radius=.05) 108 red_point = Dot(point=[0, 0, 0], color=RED, radius=.05) 109 yellow_point= Dot(point=[0, 0, 0], color=YELLOW, radius=.05) 110 111 self.add(blue_point, red_point, yellow_point) 112 113 # Store the points and their animations to move to target positions 114 animations.append(blue_point.animate.move_to(target_blue)) 115 animations.append(red_point.animate.move_to(target_red)) 116 animations.append(yellow_point.animate.move_to(target_yellow)) 117 118 # Play animations to move points to their target positions 119 self.play(*animations) 120 self.wait(duration=1) 121 122 123 text = Text("Impurity = 0.666").move_to([0,3,0]) 124 125 self.play(Create(text)) 126 127 self.wait(duration=1) 128 129 self.wait(1) 130 131 line = Line(start=[-4, 0,0], end=[4,0,0]) 132 self.play(Create(line)) 133 134 self.wait(2) 135 136 137 t1 = Rectangle(width=8).set_fill(BLUE, opacity=.2).move_to([0,1,0]) 138 139 m1 = Rectangle(width=8).set_fill(RED, opacity=.2).move_to([0,-1,0]) 140 141 self.play(GrowFromCenter(t1)) 142 self.play(GrowFromCenter(m1)) 143 144 self.wait(duration=1) 145 146 text3 = Text("Impurity = 0.5").move_to([0,3,0]) 147 self.play(Transform(text,text3)) 148 149 150 line = Line(start=[0, 0,0], end=[0,-2,0]) 151 self.play(Create(line)) 152 153 self.play(m1.animate.stretch(.5, dim=0).align_to([0, -1, 0], RIGHT)) 154 155 rb = Rectangle(width=4).set_fill(YELLOW, opacity=.3).align_to([0,-1,0], LEFT) 156 rb.shift(DOWN) 157 158 self.play(GrowFromCenter(rb)) 159 160 161 text4 = Text("Impurity = 0.0").move_to([0,3,0]) 162 self.play(Transform(text,text4)) 163 self.wait(duration=2)