给一条线段加括号标注,是几何讲解里最常见的需求。示例先用 Brace(line) 生成水平括号并用 get_text() 配文字,再沿线段法向生成第二个括号、用 get_tex() 配 LaTeX 标注。

完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
from manim import *
class BraceAnnotation(Scene):
def construct(self):
dot = Dot([-2, -1, 0])
dot2 = Dot([2, 1, 0])
line = Line(dot.get_center(), dot2.get_center()).set_color(ORANGE)
b1 = Brace(line)
b1text = b1.get_text("Horizontal distance")
b2 = Brace(line, direction=line.copy().rotate(PI / 2).get_unit_vector())
b2text = b2.get_tex("x-x_1")
self.play(*[FadeIn(_o) for _o in [line, dot, dot2, b1, b2, b1text, b2text]], run_time=1.0)
self.wait(1)
self.wait(3.60)

演示视频

下载 Python 脚本

要点

  • Brace(mobject, direction=...) 的括号长度自动匹配目标对象
  • get_text() 输出普通文本,get_tex() 输出公式,按内容选
  • 括号方向由 direction 决定,常用 UP / DOWN 或对象的单位法向量

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