角度标注最难的是「线一动,弧和标签要跟着动」。示例用 ValueTracker 存角度,给动线、角度弧、\theta 标签各挂一个 updater,让三者每帧重算,最后把角度从 110° 拉到 350°。

完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from manim import *
class MovingAngle(Scene):
def construct(self):
rotation_center = LEFT
theta_tracker = ValueTracker(110)
line1 = Line(LEFT, RIGHT)
line_moving = Line(LEFT, RIGHT)
line_ref = line_moving.copy()
line_moving.rotate(
theta_tracker.get_value() * DEGREES, about_point=rotation_center
)
a = Angle(line1, line_moving, radius=0.5, other_angle=False)
tex = MathTex(r"\theta").move_to(
Angle(
line1, line_moving, radius=0.5 + 3 * SMALL_BUFF, other_angle=False
).point_from_proportion(0.5)
)
self.play(*[FadeIn(_o) for _o in [line1, line_moving, a, tex]], run_time=1.0)
self.wait()
line_moving.add_updater(
lambda x: x.become(line_ref.copy()).rotate(
theta_tracker.get_value() * DEGREES, about_point=rotation_center
)
)
a.add_updater(
lambda x: x.become(Angle(line1, line_moving, radius=0.5, other_angle=False))
)
tex.add_updater(
lambda x: x.move_to(
Angle(
line1, line_moving, radius=0.5 + 3 * SMALL_BUFF, other_angle=False
).point_from_proportion(0.5)
)
)
self.play(theta_tracker.animate.set_value(40))
self.play(theta_tracker.animate.increment_value(140))
self.play(tex.animate.set_color(RED), run_time=0.5)
self.play(theta_tracker.animate.set_value(350))
self.wait(0.40)

演示视频

下载 Python 脚本

要点

  • Angle(line1, line2, radius=...) 依据两条线的交点自动画弧
  • x.become(...) 在 updater 里重建对象,是「整对象重算」的常用写法
  • point_from_proportion(0.5) 取弧的中点,用来放角度标签

代码来源:Manim Community 官方 Example Gallery 示例 MovingAngle(MIT),已在 Manim CE 0.20.1 实跑验证并生成上方视频。