Skip to content

Commit 3d1b1a1

Browse files
feat(manim): add custom sine/cosine plot
1 parent 9fd029b commit 3d1b1a1

File tree

1 file changed

+150
-142
lines changed

1 file changed

+150
-142
lines changed

visualization/manim_exp/more.py

Lines changed: 150 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -2,192 +2,200 @@
22

33

44
class MovingFrame(Scene):
5-
def construct(self):
6-
# Write equations
7-
equation = MathTex("2x^2-5x+2", "=", "(x-2)(2x-1)")
5+
def construct(self):
6+
# Write equations
7+
equation = MathTex("2x^2-5x+2", "=", "(x-2)(2x-1)")
88

9-
# Create animation
10-
self.play(Write(equation))
9+
# Create animation
10+
self.play(Write(equation))
1111

12-
# Add moving frames
13-
framebox1 = SurroundingRectangle(equation[0], buff=.1)
14-
framebox2 = SurroundingRectangle(equation[2], buff=.1)
12+
# Add moving frames
13+
framebox1 = SurroundingRectangle(equation[0], buff=0.1)
14+
framebox2 = SurroundingRectangle(equation[2], buff=0.1)
1515

16-
# Create animations
17-
self.play(Create(framebox1)) # creating the frame
16+
# Create animations
17+
self.play(Create(framebox1)) # creating the frame
1818

19-
self.wait()
20-
# replace frame 1 with frame 2
21-
self.play(ReplacementTransform(framebox1, framebox2))
19+
self.wait()
20+
# replace frame 1 with frame 2
21+
self.play(ReplacementTransform(framebox1, framebox2))
2222

23-
self.wait()
23+
self.wait()
2424

2525

2626
class MathematicalEquation(Scene):
27-
def construct(self):
28-
# Write equations
29-
equation1 = MathTex("2x^2-5x+2")
30-
eq_sign_1 = MathTex("=")
31-
equation2 = MathTex("2x^2-4x-x+2")
32-
eq_sign_2 = MathTex("=")
33-
equation3 = MathTex("(x-2)(2x-1)")
27+
def construct(self):
28+
# Write equations
29+
equation1 = MathTex("2x^2-5x+2")
30+
eq_sign_1 = MathTex("=")
31+
equation2 = MathTex("2x^2-4x-x+2")
32+
eq_sign_2 = MathTex("=")
33+
equation3 = MathTex("(x-2)(2x-1)")
3434

35-
# Put each equation or sign in the appropriate positions
36-
equation1.next_to(eq_sign_1, LEFT)
37-
equation2.next_to(eq_sign_1, RIGHT)
35+
# Put each equation or sign in the appropriate positions
36+
equation1.next_to(eq_sign_1, LEFT)
37+
equation2.next_to(eq_sign_1, RIGHT)
3838

39-
eq_sign_2.shift(DOWN)
40-
equation3.shift(DOWN)
39+
eq_sign_2.shift(DOWN)
40+
equation3.shift(DOWN)
4141

42-
# Align bottom equations with the top equations
43-
eq_sign_2.align_to(eq_sign_1, LEFT)
44-
equation3.align_to(equation2, LEFT)
42+
# Align bottom equations with the top equations
43+
eq_sign_2.align_to(eq_sign_1, LEFT)
44+
equation3.align_to(equation2, LEFT)
4545

46-
# Group equations and sign
47-
eq_group = VGroup(equation1, eq_sign_1, equation2, eq_sign_2, equation3)
46+
# Group equations and sign
47+
eq_group = VGroup(equation1, eq_sign_1, equation2, eq_sign_2, equation3)
48+
49+
# Create animation
50+
self.play(Write(eq_group))
51+
self.wait()
4852

49-
# Create animation
50-
self.play(Write(eq_group))
51-
self.wait()
5253

5354
class MovingAndZoomingCamera(MovingCameraScene):
54-
def construct(self):
55-
# Write equations
56-
equation = MathTex("2x^2-5x+2", "=", "(x-2)(2x-1)")
55+
def construct(self):
56+
# Write equations
57+
equation = MathTex("2x^2-5x+2", "=", "(x-2)(2x-1)")
58+
59+
self.add(equation)
60+
self.play(
61+
self.camera.frame.animate.move_to(equation[0]).set(
62+
width=equation[0].width * 2
63+
)
64+
)
65+
self.wait(0.3)
66+
self.play(
67+
self.camera.frame.animate.move_to(equation[2]).set(
68+
width=equation[2].width * 2
69+
)
70+
)
5771

58-
self.add(equation)
59-
self.play(self.camera.frame.animate.move_to(equation[0]).set(width=equation[0].width*2))
60-
self.wait(0.3)
61-
self.play(self.camera.frame.animate.move_to(equation[2]).set(width=equation[2].width*2))
6272

6373
class Graph(Scene):
64-
def construct(self):
65-
axes = Axes(
66-
x_range=[-3, 3, 1],
67-
y_range=[-5, 5, 1],
68-
x_length=6,
69-
y_length=6,
70-
)
71-
72-
# Add labels
73-
axes_labels = axes.get_axis_labels(x_label="x", y_label="f(x)")
74-
75-
# Create function graphs
76-
graph = axes.plot(lambda x: x**2, color=BLUE)
77-
graph_label = axes.get_graph_label(graph, label="x^2")
78-
79-
self.add(axes, axes_labels)
80-
self.play(Create(graph))
81-
self.play(Write(graph_label))
82-
self.wait()
74+
def construct(self):
75+
axes = Axes(
76+
x_range=[-3, 3, 1],
77+
y_range=[-5, 5, 1],
78+
x_length=6,
79+
y_length=6,
80+
)
81+
82+
# Add labels
83+
axes_labels = axes.get_axis_labels(x_label="x", y_label="f(x)")
84+
85+
# Create function graphs
86+
graph = axes.plot(lambda x: x**2, color=BLUE)
87+
graph_label = axes.get_graph_label(graph, label="x^2")
88+
89+
self.add(axes, axes_labels)
90+
self.play(Create(graph))
91+
self.play(Write(graph_label))
92+
self.wait()
93+
8394

8495
class GroupCircles(Scene):
85-
def construct(self):
96+
def construct(self):
97+
# Create circles
98+
circle_green = Circle(color=GREEN)
99+
circle_blue = Circle(color=BLUE)
100+
circle_red = Circle(color=RED)
101+
102+
# Set initial positions
103+
circle_green.shift(LEFT)
104+
circle_blue.shift(RIGHT)
86105

87-
# Create circles
88-
circle_green = Circle(color=GREEN)
89-
circle_blue = Circle(color=BLUE)
90-
circle_red = Circle(color=RED)
106+
# Create 2 different groups
107+
gr = VGroup(circle_green, circle_red)
108+
gr2 = VGroup(circle_blue)
109+
self.add(gr, gr2) # add two groups to the scene
110+
self.wait()
91111

92-
# Set initial positions
93-
circle_green.shift(LEFT)
94-
circle_blue.shift(RIGHT)
112+
self.play((gr + gr2).animate.shift(DOWN)) # shift 2 groups down
95113

96-
# Create 2 different groups
97-
gr = VGroup(circle_green, circle_red)
98-
gr2 = VGroup(circle_blue)
99-
self.add(gr, gr2) # add two groups to the scene
100-
self.wait()
114+
self.play(gr.animate.shift(RIGHT)) # move only 1 group
115+
self.play(gr.animate.shift(UP))
101116

102-
self.play((gr + gr2).animate.shift(DOWN)) # shift 2 groups down
117+
self.play((gr + gr2).animate.shift(RIGHT)) # shift 2 groups to the right
118+
self.play(circle_red.animate.shift(RIGHT))
119+
self.wait()
103120

104-
self.play(gr.animate.shift(RIGHT)) # move only 1 group
105-
self.play(gr.animate.shift(UP))
106121

107-
self.play((gr + gr2).animate.shift(RIGHT)) # shift 2 groups to the right
108-
self.play(circle_red.animate.shift(RIGHT))
109-
self.wait()
122+
class RollingCircleTrace(Scene):
123+
def construct(self):
124+
# Create circle and dot
125+
circ = Circle(color=BLUE).shift(4 * LEFT)
126+
dot = Dot(color=BLUE).move_to(circ.get_start())
110127

111-
class TracedPathExample(Scene):
112-
def construct(self):
113-
# Create circle and dot
114-
circ = Circle(color=BLUE).shift(4*LEFT)
115-
dot = Dot(color=BLUE).move_to(circ.get_start())
128+
# Group dot and circle
129+
rolling_circle = VGroup(circ, dot)
130+
trace = TracedPath(circ.get_start)
116131

117-
# Group dot and circle
118-
rolling_circle = VGroup(circ, dot)
119-
trace = TracedPath(circ.get_start)
132+
rolling_circle.add_updater(lambda m: m.rotate(-0.3)) # Rotate the circle
120133

121-
rolling_circle.add_updater(lambda m: m.rotate(-0.3)) # Rotate the circle
134+
self.add(trace, rolling_circle) # add trace and rolling circle to the scene
122135

123-
self.add(trace, rolling_circle) # add trace and rolling circle to the scene
136+
# Shift the circle to 8*RIGHT
137+
self.play(rolling_circle.animate.shift(8 * RIGHT), run_time=4, rate_func=linear)
124138

125-
# Shift the circle to 8*RIGHT
126-
self.play(rolling_circle.animate.shift(8*RIGHT), run_time=4, rate_func=linear)
127139

128140
class WriteEquation(Scene):
129-
def construct(self):
130-
equation = MathTex(r"e^{i\pi} + 1 = 0")
141+
def construct(self):
142+
equation = MathTex(r"e^{i\pi} + 1 = 0")
143+
144+
self.play(Write(equation))
145+
self.wait()
131146

132-
self.play(Write(equation))
133-
self.wait()
134147

135148
class EquationSteps(Scene):
136-
def construct(self):
137-
step1 = MathTex(r"2x + 5 = 13")
138-
step2 = MathTex(r"2x = 8")
139-
step3 = MathTex(r"x = 4")
140-
141-
self.play(Write(step1))
142-
self.wait()
143-
self.play(Transform(step1, step2))
144-
self.wait()
145-
self.play(Transform(step1, step3))
146-
self.wait()
149+
def construct(self):
150+
step1 = MathTex(r"2x + 5 = 13")
151+
step2 = MathTex(r"2x = 8")
152+
step3 = MathTex(r"x = 4")
153+
154+
self.play(Write(step1))
155+
self.wait()
156+
self.play(Transform(step1, step2))
157+
self.wait()
158+
self.play(Transform(step1, step3))
159+
self.wait()
160+
147161

148162
class MovingCamera(MovingCameraScene):
149-
def construct(self):
150-
equation = MathTex(
151-
r"\frac{d}{dx}(x^2) = 2x"
152-
)
163+
def construct(self):
164+
equation = MathTex(r"\frac{d}{dx}(x^2) = 2x")
165+
166+
self.play(Write(equation))
167+
self.wait()
153168

154-
self.play(Write(equation))
155-
self.wait()
169+
# Zoom in on the derivative
170+
self.play(self.camera.frame.animate.scale(0.5).move_to(equation[0]))
171+
self.wait()
156172

157-
# Zoom in on the derivative
158-
self.play(
159-
self.camera.frame.animate.scale(0.5).move_to(equation[0])
160-
)
161-
self.wait()
162173

163174
class MoveObjectsTogether(Scene):
164-
def construct(self):
165-
square = Square(color=BLUE)
166-
circle = Circle(color=RED)
175+
def construct(self):
176+
square = Square(color=BLUE)
177+
circle = Circle(color=RED)
167178

168-
# Group objects
169-
group = VGroup(square, circle)
170-
group.arrange(RIGHT, buff=1)
179+
# Group objects
180+
group = VGroup(square, circle)
181+
group.arrange(RIGHT, buff=1)
171182

172-
self.play(Create(group))
173-
self.wait()
183+
self.play(Create(group))
184+
self.wait()
185+
186+
# Move the entire group
187+
self.play(group.animate.shift(UP * 2))
188+
self.wait()
174189

175-
# Move the entire group
176-
self.play(group.animate.shift(UP * 2))
177-
self.wait()
178190

179191
class TracePath(Scene):
180-
def construct(self):
181-
dot = Dot(color=RED)
182-
183-
# Create traced path
184-
path = TracedPath(dot.get_center, stroke_color=BLUE, stroke_width=4)
185-
self.add(path, dot)
186-
187-
# Move the dot in a circular pattern
188-
self.play(
189-
MoveAlongPath(dot, Circle(radius=2)),
190-
rate_func=linear,
191-
run_time=4
192-
)
193-
self.wait()
192+
def construct(self):
193+
dot = Dot(color=RED)
194+
195+
# Create traced path
196+
path = TracedPath(dot.get_center, stroke_color=BLUE, stroke_width=4)
197+
self.add(path, dot)
198+
199+
# Move the dot in a circular pattern
200+
self.play(MoveAlongPath(dot, Circle(radius=2)), rate_func=linear, run_time=4)
201+
self.wait()

0 commit comments

Comments
 (0)