
在Manim中,ParametricFunction是一个用于绘制参数方程曲线的核心工具,它允许用户通过一个参数(通常用t表示)来同时定义x、y、z坐标,从而创建那些无法用单一函数y=f(x)形式表示的复杂曲线,如圆、螺线、利萨如图形等三维或二维轨迹。这个类接收一个返回坐标元组的函数作为输入,并支持自定义参数范围、颜色、线宽等属性,特别适合可视化数学中的参数方程描述的运动轨迹、几何曲线和动态系统。
1.基本介绍
ParametricFunction 用于创建参数方程定义的曲线。与普通函数不同,参数方程使用一个参数(通常为 t)同时定义 x 和 y 坐标。
2.主要参数
function:参数方程函数,接受一个参数(通常为 t),返回一个三维坐标元组 (x, y, z)
t_range:参数范围,格式为 [起始值, 结束值, 步长],默认为 [0, 1, 0.01]
- color
:曲线颜色
- stroke_width
:线条宽度
- dt
:微分步长(影响导数计算)
- discontinuities
:不连续点列表
- use_smoothing`:是否使用平滑处理
3.实例代码展示
示例1:创建一个圆
创建一个圆,圆是典型的参数方程构造的图形,下面来看代码示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| from manim import * import numpy as np
class SimpleParametric(Scene): def construct(self): # 定义参数方程:圆 def circle(t): return np.array([np.cos(t), np.sin(t), 0]) # 创建参数曲线 parametric = ParametricFunction( circle, t_range=[0, 2*PI, 0.01], color=BLUE, stroke_width=3 ) self.play(Create(parametric)) self.wait()
|
示例 2:更复杂的参数曲线-利萨如图形
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 * import numpy as np
class ComplexParametric(Scene): def construct(self): # 利萨如图形 def lissajous(t): return np.array([ np.sin(3*t), np.sin(4*t + PI/6), 0 ]) curve = ParametricFunction( lissajous, t_range=[0, 2*PI, 0.005], color=RED, stroke_width=4 ) self.play(Create(curve)) self.wait()
|
示例 3:带有动画的参数曲线-螺线
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| from manim import * import numpy as np
class AnimatedParametric(Scene): def construct(self): # 螺线 def spiral(t): return np.array([ t * np.cos(5*t) / 2, t * np.sin(5*t) / 2, 0 ]) parametric = ParametricFunction( spiral, t_range=[0, 4*PI, 0.05], color=GREEN, stroke_width=3 ) # 创建绘制动画 self.play(Create(parametric, run_time=3)) self.wait()
|
示例 4:3D 参数曲线
创建一个螺旋曲线
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 * import numpy as np
class Parametric3D(ThreeDScene): def construct(self): # 设置相机 self.set_camera_orientation(phi=75*DEGREES, theta=30*DEGREES) # 3D 螺旋线 def helix(t): return np.array([ np.cos(5*t), np.sin(5*t), t/2 ]) curve = ParametricFunction( helix, t_range=[0, 4*PI, 0.05], color=YELLOW, stroke_width=3 ) axes = ThreeDAxes() self.play(Create(axes), Create(curve)) self.begin_ambient_camera_rotation(rate=0.2) self.wait(4)
|
示例 5:使用 lambda 函数
使用 lambda 表达式直接定义参数方程,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| from manim import * import numpy as np
class LambdaParametric(Scene): def construct(self): # 使用 lambda 表达式直接定义参数方程 curve = ParametricFunction( lambda t: np.array([ np.cos(t) + np.cos(6*t)/2 + np.sin(14*t)/3, np.sin(t) + np.sin(6*t)/2 + np.cos(14*t)/3, 0 ]), t_range=[0, 2*PI, 0.001], color=PURPLE, stroke_width=3 ) self.play(Create(curve)) self.wait()
|
示例6:四叶玫瑰线参数方程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| from manim import * import numpy as np
class ParametricFunctionExample(Scene): def construct(self): # 四叶玫瑰线参数方程: r = sin(2θ) 的笛卡尔形式 # x = sin(2t)cos(t), y = sin(2t)sin(t) rose_curve = ParametricFunction( lambda t: np.array( [ np.sin(2 * t) * np.cos(t), # x(t) np.sin(2 * t) * np.sin(t), # y(t) 0, ] ), t_range=[0, 2 * PI], # 完整周期 color=RED, stroke_width=4, )
self.play(Create(rose_curve)) self.wait()
|
4.总结
ParametricFunction 适合绘制不能用单一函数 y=f(x) 表示的曲线
- 合理选择
t_range 的步长以平衡视觉效果和性能
- 可以使用
discontinuities 参数处理不连续点
- 结合
ThreeDScene 可以创建三维参数曲线
- 利用导数方法可以添加切线等几何元素
通过灵活运用 ParametricFunction,你可以创建各种复杂的数学曲线和可视化效果。