Manim中三种函数图像类的比较

Manim 库中,FunctionGraphImplicitFunctionParametricFunction 都是用于绘制函数图像的类,但它们的适用场景、输入形式和实现方式有显著区别。

以下是详细对比:

1. FunctionGraph

  • 用途 :绘制 显式函数 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> y = f ( x ) y = f(x) </math>y=f(x)) 的图像(单值函数)。
  • 输入要求
    接受一个 一元函数 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> f ( x ) f(x) </math>f(x)) 和 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> x x </math>x) 的范围(如x_range=[-2, 2])。
  • 特点
    • 直接映射 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> x → ( x , f ( x ) ) x \to (x, f(x)) </math>x→(x,f(x)))。
    • 要求函数是 单值 的(一个 <math xmlns="http://www.w3.org/1998/Math/MathML"> x x </math>x对应唯一 <math xmlns="http://www.w3.org/1998/Math/MathML"> y y </math>y)。
  • 示例代码
python 复制代码
class Example(Scene):
    def construct(self):
        # 绘制 y = x^2
        graph = FunctionGraph(lambda x: x**2, x_range=[-2, 2], color=BLUE)
        self.add(graph)

2. ImplicitFunction

  • 用途 :绘制 隐函数 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> F ( x , y ) = 0 F(x, y) = 0 </math>F(x,y)=0) 的图像(如圆、椭圆)。
  • 输入要求
    接受一个 二元函数 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> F ( x , y ) F(x, y) </math>F(x,y)) 和 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> x , y x, y </math>x,y) 的范围(如x_range=[-3, 3], y_range=[-3, 3])。
  • 特点
    • 通过数值方法(如 Marching Squares 算法)求解满足 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> F ( x , y ) = 0 F(x, y) = 0 </math>F(x,y)=0) 的点集。
    • 可绘制 多值曲线 (如一个 <math xmlns="http://www.w3.org/1998/Math/MathML"> x x </math>x对应多个$y ))。
  • 示例代码
python 复制代码
class ImplicitFunctionExample(Scene):
    def construct(self):
        # 笛卡尔叶形线隐函数方程: x^3 + y^3 - 3axy = 0 (取 a=1)
        cartesian_leaf = ImplicitFunction(
            lambda x, y: x**3 + y**3 - 3 * x * y,  # F(x,y) = x³ + y³ - 3xy
            x_range=[-2, 2],
            y_range=[-2, 2],
            color=BLUE,
            stroke_width=3,
        )

        self.add(cartesian_leaf)

笛卡尔叶形线 (一种自交曲线),可以展示隐函数处理 多值曲线 的能力(一个x对应多个y)。

3. ParametricFunction

  • 用途 :绘制 参数方程 定义的曲线(如螺旋线、摆线)。
  • 输入要求
    接受一个 向量值函数 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> r ( t ) = ( x ( t ) , y ( t ) ) \mathbf{r}(t) = (x(t), y(t)) </math>r(t)=(x(t),y(t))) 和参数 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> t t </math>t) 的范围(如t_range=[0, T])。
  • 特点
    • 通过参数 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> t t </math>t) 映射到点 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( x ( t ) , y ( t ) ) (x(t), y(t)) </math>(x(t),y(t))。
    • 可绘制 任意参数化曲线(包括封闭曲线、自交曲线)。
  • 示例代码
python 复制代码
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.add(rose_curve)

四叶玫瑰线 展示参数方程处理 封闭曲线 的能力,通过参数 <math xmlns="http://www.w3.org/1998/Math/MathML"> t t </math>t直接控制曲线生成过程。

4. 核心区别总结

特性 FunctionGraph ImplicitFunction ParametricFunction
输入形式 <math xmlns="http://www.w3.org/1998/Math/MathML"> y = f ( x ) y = f(x) </math>y=f(x) <math xmlns="http://www.w3.org/1998/Math/MathML"> F ( x , y ) = 0 F(x, y) = 0 </math>F(x,y)=0 <math xmlns="http://www.w3.org/1998/Math/MathML"> r ( t ) = ( x ( t ) , y ( t ) ) \mathbf{r}(t) = (x(t), y(t)) </math>r(t)=(x(t),y(t))
函数类型 显式函数(单值) 隐函数(多值) 参数方程
适用场景 简单函数(如 <math xmlns="http://www.w3.org/1998/Math/MathML"> y = sin ⁡ x ) y = \sin x) </math>y=sinx) 复杂曲线(如椭圆、心形线) 任意参数化曲线(如螺旋线)
计算复杂度 低(直接计算) 高(数值求解) 中(采样计算)
多值支持 ❌ 不支持 ✔️ 支持 ✔️ 支持

5. 如何选择?

  • FunctionGraph 当函数能显式写成 <math xmlns="http://www.w3.org/1998/Math/MathML"> y = f ( x ) y = f(x) </math>y=f(x)时(如多项式、三角函数)。
  • ImplicitFunction 当函数以方程 <math xmlns="http://www.w3.org/1998/Math/MathML"> F ( x , y ) = 0 F(x, y) = 0 </math>F(x,y)=0 给出时(如圆、椭圆)。
  • ParametricFunction 当曲线用参数 ( t ) 描述时(如摆线、贝塞尔曲线)。

通过理解这些差异,你可以根据函数的具体形式高效选择对应的 Manim 类进行绘制。

相关推荐
风逸hhh1 小时前
python打卡day58@浙大疏锦行
开发语言·python
烛阴2 小时前
一文搞懂 Python 闭包:让你的代码瞬间“高级”起来!
前端·python
JosieBook2 小时前
【Java编程动手学】Java中的数组与集合
java·开发语言·python
Gyoku Mint3 小时前
深度学习×第4卷:Pytorch实战——她第一次用张量去拟合你的轨迹
人工智能·pytorch·python·深度学习·神经网络·算法·聚类
郭庆汝9 小时前
pytorch、torchvision与python版本对应关系
人工智能·pytorch·python
思则变12 小时前
[Pytest] [Part 2]增加 log功能
开发语言·python·pytest
漫谈网络12 小时前
WebSocket 在前后端的完整使用流程
javascript·python·websocket
try2find14 小时前
安装llama-cpp-python踩坑记
开发语言·python·llama
博观而约取15 小时前
Django ORM 1. 创建模型(Model)
数据库·python·django
精灵vector16 小时前
构建专家级SQL Agent交互
python·aigc·ai编程