(目录)
一、画点和线
下边代码为实验的框架
# -*- coding: utf-8 -*-
import os
from manim import *
class MyAnim(Scene):
def a_grid(self): # 画网格坐标
grid = NumberPlane(
x_range=(-7, 7, 1), # x轴范围
y_range=(-4, 4, 1), # y轴范围
x_length=10, # x轴长度
y_length=6, # y轴长度
background_line_style={
"stroke_color": TEAL,
"stroke_width": 4,
"stroke_opacity": 0.6,
},
faded_line_style={"stroke_color": GRAY, "stroke_opacity": 0.3},
faded_line_ratio=0.8, # 淡化线与背景线的比例
make_smooth_after_applying_functions=True, # 启用平滑处理
)
self.add(grid)
def a_dot(self): # 画点
for x in range(-3, 0):
for y in range(3, 0, -1):
p = Dot([x, y, 0])
self.add(p)
def a_line(self): # 画线
l1 = Line([-1, -1, 0], [1, 1, 0])
self.add(l1)
l2 = Line([-1, 1, 0], [1, -1, 0])
self.add(l2)
def construct(self): # 构造场景
self.a_grid()
self.a_dot()
self.a_line()
self.wait(10)
# 运行场景
if __name__ == "__main__":
demo_file = os.path.basename(__file__)
os.system(f"manim {demo_file} MyAnim -p")
如果self.add(),是不带动画效果的;如果要想带上动画效果可以使用self.play() 比如使用:
self.play(Create(p), run_time=0.5)
二、画箭头与虚线
def a_arrow(self):
a = Arrow([-1, 1, 0], [1, 1, 0])
self.play(Create(a), run_time=0.5)
a = Arrow([-1, 0, 0], [1, 0, 0])
self.play(Create(a), run_time=0.5)
a = Arrow([-1, -1, 0], [1, -1, 0])
self.play(Create(a), run_time=0.5)
def a_dashedLine(self):
dl = DashedLine([-1, 1, 0], [1, 1, 0])
self.play(Create(dl), run_time=0.5)
dl = DashedLine([-1, 0, 0], [1, 0, 0])
self.play(Create(dl), run_time=0.5)
dl = DashedLine([-1, -1, 0], [1, -1, 0])
self.play(Create(dl), run_time=0.5)
三、画圆与椭圆
- 画圆只要提供半径即可,圆心默认在屏幕的中心(0,0,0)。
- 画椭圆的两个参数 width和 height分别控制椭圆最大宽度和最大高度。
def a_Circle(self):
c = Circle(radius=1)
self.play(Create(c), run_time=0.5)
def a_Ellipse(self):
e = Ellipse(width=2, height=1)
self.play(Create(e), run_time=0.5)
四、画圆弧
画圆弧主要有三个参数:
- angle:圆弧的弧度
- start_angle: 开始的角度,默认 0
- radius:圆弧的半径
def a_Arc(self):
# 180度圆弧,半径2
a = Arc(angle=PI, radius=2)
self.play(Create(a), run_time=0.5)
五、画多边形
def a_Triangle(self):
# 等边三角形
t = Triangle()
self.play(Create(t))
def a_Square(self):
# 正方形,side_length 是正方形的边长
s = Square(side_length=1.5)
self.play(Create(s), run_time=0.5)
def a_Rectangle(self):
# 矩形,矩形的高为 height,宽为 width
r = Rectangle(width=2, height=1.5)
self.play(Create(r), run_time=0.5)
def a_RoundedRectangle(self):
# 圆角矩形,参数corner_radius用来控制矩形四个角的弧度半径
r = RoundedRectangle(corner_radius=0.4, width=2, height=1.5)
self.play(Create(r), run_time=0.5)
def a_Polygon(self):
# 多边形
p = Polygon([1, 1, 0], [2, 0, 0], [3, 1, 0], [3, -1, 0], [1, -1, 0])
self.play(Create(p), run_time=0.5)
def a_RegularPolygon(self):
# 正多边形
p1 = RegularPolygon(n=6) # 正六边形
p2 = RegularPolygon(n=8) # 正八边形
p3 = RegularPolygon(n=10) # 正十边形
vg = VGroup(p1, p2, p3)
vg.arrange(RIGHT, buff=SMALL_BUFF)
self.play(Create(vg))
六、坐标系
1. 数轴:NumberLine
数轴(NumberLine)是最基本的一维坐标系,它的关键参数是:
- x_range:设置数轴的范围和间隔
- length:设置数轴显示的长度
NumberLine(x_range=[-10, 10, 2], length=10, include_numbers=True)
NumberLine(x_range=[-3, 3, 0.5], length=12, include_numbers=True)
NumberLine(
x_range=[-5, 5 + 1, 1],
length=6,
include_numbers=True,
include_tip=True,
rotation=10 * DEGREES,
)
2. 实数平面:NumberPlane
实数平面(NumberPlane)的关键参数有4个:
- x_range:设置X轴的范围和间隔
- y_range:设置Y轴的范围和间隔
- x_length:设置X轴显示的长度
- y_length:设置Y轴显示的长度
NumberPlane(
x_range=(-4, 11, 1),
y_range=(-3, 3, 1),
x_length=3,
y_length=2,
)
NumberPlane(
x_range=(-4, 11, 1),
x_length=3,
y_length=4,
)
3. 复数平面:ComplexPlane
复数平面(ComplexPlane)是基于实数平面(NumberPlane)的,参数类似,只是多了一些标记复数的信息。
plane = ComplexPlane().add_coordinates()
d1 = Dot(plane.n2p(2 + 1j), color=YELLOW)
d2 = Dot(plane.n2p(-3 - 2j), color=YELLOW)
label1 = Tex("2+i").next_to(d1, UR, 0.1)
label2 = Tex("-3-2i").next_to(d2, UR, 0.1)
4. 极坐标系:PolarPlane
极坐标系(PolarPlane)通过角度和与原点的距离来定位位置,经常被用于导航类的系统中,与直角坐标相比,在这类系统中能极大的简化计算。 它的关键参数有:
- azimuth_step:分割的角度个数
- size:极坐标在屏幕中显示的大小
- radius_step:极坐标半径的间隔
- radius_max:极坐标最大半径
plane = PolarPlane(
azimuth_step=30,
size=6,
radius_step=1,
radius_max=3,
).add_coordinates()
5. 二维笛卡尔坐标系:Axes
二维的笛卡尔坐标系(Axes)使用的比较多,它在平面坐标系之上,又提供了更多的配置,可以更加灵活的配置数轴,除了上面平面坐标系提到的那4个关键参数之外,还有2个配置坐标轴的参数也很重要:
- x_axis_config:配置X轴如何显示的参数
- y_axis_config:配置Y轴如何显示的参数 比如下面的示例中, 配置了与X轴不一样刻度的Y轴。
ax = Axes(
x_range=[0, 10, 1],
y_range=[-2, 6, 1],
x_length=6,
tips=False,
axis_config={"include_numbers": True},
y_axis_config={"scaling": LogBase(custom_labels=True)},
)
# x_min 必须 > 0,因为 x=0 时,y是负无穷
graph = ax.plot(lambda x: x**2, x_range=[0.001, 10], use_smoothing=False)
6. 三维笛卡尔坐标系:ThreeDAxes
三维的笛卡尔坐标系(ThreeDAxes)与二维坐标系的参数类似,只是多了一个维度(Z轴)的配置,其配置参数与X轴和Y轴类似。 显示三维图形时,有2点需要额外注意:
- 一是场景要继承 ThreeDScene。
- 二是要调整下默认的相机位置,也就是视角的位置,默认视角是从Z轴顶部向下看的。
# 默认的相机视角
class CoordinateSample(ThreeDScene):
def construct(self):
axes = ThreeDAxes(y_length=8)
circle = Circle(color=BLUE, radius=2)
vg = VGroup(axes, circle)
self.play(Create(vg), run_time=2)
self.wait()
#####################################################################
# 调整的相机视角
class CoordinateSample(ThreeDScene):
def construct(self):
axes = ThreeDAxes(y_length=8)
circle = Circle(color=BLUE, radius=2)
vg = VGroup(axes, circle)
# 调整相机视角的代码 phi是与Z轴之间的角度,theta是围绕Z轴旋转的角度
self.set_camera_orientation(phi=75 * DEGREES, theta=30 * DEGREES)
self.play(Create(vg), run_time=2)
self.wait()