在manim的丰富图形库中,圆形类是一个基础且强大的模块。
无论是简单的圆形绘制,还是复杂的圆形变换,它都能以简洁的代码实现。

manim中圆形类的相关模块主要有3个:

  1. Circle:标准的圆形
  2. Annulus:圆环形状
  3. Ellipse:椭圆形状

其中,Annulus和Ellipse继承自Circle。
manim边学边做--圆形类
圆形系列处于manim中的Mobject之下。

1. 主要参数

圆Circle对象的属性不多,主要有:

参数名称 类型 说明
radius float 半径
color str 颜色
stroke_width float 边框厚度
fill_opacity float 透明度

圆环Annulus对象的主要属性有:

参数名称 类型 说明
inner_radius float 内圈半径
outer_radius float 外圈半径
color str 颜色
mark_paths_closed bool TODO

Annulus会渲染inner_radius和outer_radius之间的部分,也就是一个环。

椭圆Ellipse对象的主要属性有:

参数名称 类型 说明
width float 椭圆宽度
height float 椭圆高度

椭圆的宽度和高度设置成一样的时候,就是标准的圆。

2. 主要方法

圆Circle对象有3个常用的方法:

名称 说明
from_three_points 根据3个点画圆
point_at_angle 返回某个点在圆上的位置
surround 修改一个圆,使其围绕给定的对象

上面方法的使用方法参见下面的使用示例章节。

圆环Annulus和椭圆Ellipse没有常用的方法。

3. 使用示例

3.1. 大小和颜色

对于圆Circle来说,通过半径radius来调节大小;
对于圆环Annulus来说,通过内半径inner_radius和外半径outer_radius来调节环的大小和粗细;
对于椭圆Ellipse来说,通过width和height来调节椭圆的大小和扁平程度。

三种形状的颜色都是color属性。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from manim import *
import numpy as np
from numpy import array, pi, cos, sin
run_time = 1
data = [[1, 2], [3, 4]]
def dot(pos, **kwargs):
return Dot(pos, **kwargs)
class N024_01(Scene):
def construct(self):
sc = self
self.play(*[FadeIn(_o) for _o in [Circle(radius=0.1, color=RED)]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [Circle(radius=0.3, color=BLUE)]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [Circle(radius=0.5, color=YELLOW)]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [Annulus(inner_radius=0.1, outer_radius=0.2, color=RED)]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [Annulus(inner_radius=0.4, outer_radius=0.5, color=BLUE)]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [Annulus(inner_radius=0.4, outer_radius=0.8, color=YELLOW)]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [Ellipse(width=0.3, height=0.1, color=RED)]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [Ellipse(width=0.8, height=0.4, color=BLUE)]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [Ellipse(width=1.8, height=1, color=YELLOW)]], run_time=1.0)
self.wait(0.5)

下载 Python 脚本

效果如下:

3.2. 根据三个点画圆

除了通过上面设置半径radius的方式来画圆,还可以根据任意三个点来生成一个圆形。
使用from_three_points方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from manim import *
FONT = "STLiti"
class Demo(Scene):
def construct(self):
d1 = Dot(RIGHT * 1.5, color=RED, radius=0.13)
d2 = Dot(UL * 1.8, color=BLUE, radius=0.13)
d3 = Dot(DR * 1.8, color=GREEN, radius=0.13)
pts = VGroup(d1, d2, d3)
c = Circle.from_three_points(d1.get_center(), d2.get_center(),
d3.get_center(), color=YELLOW, stroke_width=5)
VGroup(pts, c).scale(0.68).move_to(DOWN * 0.3)
self.play(FadeIn(pts), run_time=0.8)
self.play(Create(c), run_time=1.6)
head = Text("三点定圆", font=FONT, font_size=36, color=YELLOW).to_edge(UP, buff=0.5)
self.play(FadeIn(head), run_time=0.6)
self.wait(2.6)

下载 Python 脚本

3.3. 获取圆上某个点

通过方法point_at_angle,我们可以根据角度来获取圆上的某一个点。
manim将整个圆最右边的点(和圆心在同一高度上)作为角度为0∘0^\circ0∘的点,
然后逆时针方向逐渐增加角度。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from manim import *
import numpy as np
from numpy import array, pi, cos, sin
run_time = 1
data = [[1, 2], [3, 4]]
def dot(pos, **kwargs):
return Dot(pos, **kwargs)
class N024_03(Scene):
def construct(self):
sc = self
c = Circle(radius=2, color=YELLOW)
self.play(FadeIn(c), run_time=1.0)
p1 = c.point_at_angle(PI / 6)
p2 = c.point_at_angle(PI)
p3 = c.point_at_angle(3 * PI / 2)
self.wait(1)
self.wait(0.5)
self.wait(3.10)

下载 Python 脚本

3.4. 用圆环绕其他图形

最后,还有一个surround方法,它的主要作用是将圆环绕在另一个图形上。
surround方法有个buffer_factor 参数,
当buffer_factor >= 1时,圆形环绕在图形外部;当buffer_factor < 1时,圆形在图形内部。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from manim import *
import numpy as np
from numpy import array, pi, cos, sin
run_time = 1
data = [[1, 2], [3, 4]]
def dot(pos, **kwargs):
return Dot(pos, **kwargs)
class N024_04(Scene):
def construct(self):
sc = self
star = Star()
Circle().surround(star, buffer_factor=1)
vg = VGroup(Line(UP / 2, DOWN / 2), Line(LEFT / 2, RIGHT / 2))
Circle().surround(vg)
t = Triangle()
self.play(FadeIn(t), run_time=1.0)
Circle().surround(t, buffer_factor=0.3)
self.wait(1)
self.wait(0.5)
self.wait(3.10)

下载 Python 脚本

4. 附件

文中示例的完整脚本如下,与上方代码块、演示视频逐字一致;用 manim -qh 脚本.py 本地渲染即可复现。