
几何图形是manim中最重要的一类模块,manim内置了丰富的各类几何图形,本篇从最简单的点开始,逐个介绍manim中的几何模块。
manim中点相关的模块主要有3个:
Dot:通用的点
LabeledDot:在点的中间可以加一些说明文字
AnnotationDot:使用在注释场景中的点
Dot系列在manim各个模块中的位置大致如上图中所示。下面给出一个可直接运行的最小示例,完整参数含义见下表。
1 2 3 4 5 6 7 8 9 10 11
| from manim import * class DotSignature(Scene): """Dot 的完整参数签名(Manim CE 0.21): Dot(point=ORIGIN, radius=0.08, stroke_width=0, fill_opacity=1.0, color=WHITE) 前两个参数最常用:point 定位置,radius 定大小。 """ def construct(self): default_dot = Dot(color=WHITE).shift(2 * LEFT) bigger_dot = Dot(point=2 * RIGHT, radius=0.35, color=BLUE) self.add(default_dot, bigger_dot) self.wait(1)
|
1. 主要参数
点比较简单,所以参数不多,主要就是控制点的大小,颜色,透明度等等属性。 Dot对象:
| 参数名称 |
类型 |
说明 |
| point |
[float,float,float] |
点的坐标 |
| radius |
float |
点的半径 |
| stroke_width |
float |
边框厚度 |
| fill_opacity |
float |
透明度 |
| color |
ParsableManimColor |
颜色,可传RED等常量,也可传'#425AEF'这样的十六进制字符串 |
LabeledDot对象,继承自Dot对象,包含Dot的属性,此外还有:
| 参数名称 |
类型 |
说明 |
| label |
str / Text / MathTex |
点中的文本。字符串交给MathTex渲染,适合字母、数字和公式;中文必须传Text(中文, font="STLiti")对象,否则 LaTeX 编译报 Unicode 错 |
AnnotationDot对象,继承自Dot对象,包含Dot的属性,此外还有:
| 参数名称 |
类型 |
说明 |
| stroke_color |
str |
外框颜色 |
| fill_color |
str |
内部的颜色 |
2. 使用示例
这三种点对象中,使用最多的Dot对象,下面通过一些示例演示点的使用。
2.1. 大小
调节点的大小主要就是调节点的半径radius。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| from manim import * class DotRadius(Scene): """2.1 radius 决定点的大小。 五个点按半径从小到大横向排开,下方标出具体数值,便于对照。 """ def construct(self): title = Text("点的大小 radius", font="STLiti", font_size=34).to_edge(UP) self.play(Write(title), run_time=0.8) radii = [0.04, 0.08, 0.24, 0.32, 0.64] dots = VGroup(*[Dot(radius=r) for r in radii]).arrange(RIGHT, buff=0.9) labels = VGroup(*[ Text("r = %.2f" % r, font="STLiti", font_size=24) for r in radii ]) for lab, dot in zip(labels, dots): lab.set_y(-2.0).set_x(dot.get_x()) self.play(LaggedStart(*[FadeIn(d) for d in dots], lag_ratio=0.28), run_time=2.2) self.play(FadeIn(labels), run_time=0.6) self.wait(2.4)
|
下载 Python 脚本
2.2. 颜色
点的color属性可以设置颜色。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| from manim import * class DotColor(Scene): """2.2 color 决定点的填充色。 Dot 默认 stroke_width=0,所以整颗点就是 color 指定的颜色。 """ def construct(self): title = Text("点的颜色 color", font="STLiti", font_size=34).to_edge(UP) self.play(Write(title), run_time=0.8) specs = [ (RED, "红"), (GREEN, "绿"), (BLUE, "蓝"), (YELLOW, "黄"), (PURPLE, "紫"), ] dots = VGroup(*[Dot(radius=0.32, color=c) for c, _ in specs]) dots.arrange(RIGHT, buff=1.35) labels = VGroup(*[Text(n, font="STLiti", font_size=28) for _, n in specs]) for lab, dot in zip(labels, dots): lab.next_to(dot, DOWN, buff=0.55) self.play(LaggedStart(*[FadeIn(d) for d in dots], lag_ratio=0.25), run_time=2.0) self.play(FadeIn(labels), run_time=0.6) self.wait(2.8)
|
下载 Python 脚本
2.3. 标签
点中带标签,需要使用LabeledDot对象,它的label属性除了可以显示字母和数字之外, 还可以显示中文和数学公式。
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
| from manim import * class LabeledDotDemo(Scene): """2.3 LabeledDot 在点中央放标签。 字符串 label 会交给 MathTex 渲染(黑色),适合数字、字母、公式; 中文必须传 Text 对象并指定中文字体 STLiti,否则会渲染成豆腐块。 """ def construct(self): title = Text("带标签的点 LabeledDot", font="STLiti", font_size=34).to_edge(UP) self.play(Write(title), run_time=0.8) dots = VGroup( LabeledDot(Text("中文", font="STLiti", font_size=28, color=BLACK)), LabeledDot(r"a^2"), LabeledDot("32"), LabeledDot(r"V_i"), ).arrange(RIGHT, buff=1.2) caps = VGroup( Text("中文 Text", font="STLiti", font_size=22), Text("公式 a^2", font="STLiti", font_size=22), Text("数字 32", font="STLiti", font_size=22), Text("下标 V_i", font="STLiti", font_size=22), ) for cap, dot in zip(caps, dots): cap.next_to(dot, DOWN, buff=0.6) self.play(LaggedStart(*[FadeIn(d) for d in dots], lag_ratio=0.3), run_time=2.2) self.play(FadeIn(caps), run_time=0.6) self.wait(2.4)
|
下载 Python 脚本
label属性不能直接输入中文字符串:字符串会交给MathTex走 LaTeX 编译,中文会直接报错。正确做法是传Text对象,并指定中文字体,例如Text("中文", font="STLiti", color=BLACK);字色建议设成深色,才能衬在白色点上。输入公式则只需给 LaTeX 格式的字符串,如r"a^2"、r"V_i",输出时会自动渲染成公式。
2.4. 边框和内部
AnnotationDot对象是用来在视频中加注释和标注的,它将点分为边框和内部两部分, 边框可以设置粗细,而且这两部分还可以分别设置不同的颜色。
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
| from manim import * class AnnotationDotDemo(Scene): """2.4 AnnotationDot 把点拆成「边框 + 内部」两层。 stroke_width 控制边框厚度,stroke_color / fill_color 分别设边框与内部颜色。 """ def construct(self): title = Text("注释点 AnnotationDot", font="STLiti", font_size=34).to_edge(UP) self.play(Write(title), run_time=0.8) specs = [ (2, RED, GREEN, "细边框 2"), (10, BLUE, YELLOW, "中边框 10"), (20, YELLOW, RED, "粗边框 20"), (40, GREEN, PURPLE, "超粗边框 40"), ] dots = VGroup(*[ AnnotationDot(radius=0.50, stroke_width=w, stroke_color=sc, fill_color=fc) for w, sc, fc, _ in specs ]).arrange(RIGHT, buff=1.2) labels = VGroup(*[Text(n, font="STLiti", font_size=22) for _, _, _, n in specs]) for lab, dot in zip(labels, dots): lab.next_to(dot, DOWN, buff=0.6) self.play(LaggedStart(*[FadeIn(d) for d in dots], lag_ratio=0.3), run_time=2.2) self.play(FadeIn(labels), run_time=0.6) self.wait(2.4)
|
下载 Python 脚本
stroke_width属性设置外框的粗细,stroke_color属性设置外框的颜色,fill_color设置内部的颜色。Manim中点(Dot)是构建几何图形的基础元素,支持二维和三维场景,在这里我们主要是点(Dot)的二维场景,日后用到三维场景再给大家修改本篇文章。
主观感受,仅供参考。本文采用 本文采用 CC BY-NC-SA 4.0 许可 协议,
完整转载请注明来自
数学动画引擎。