Manim中三种函数图像类的比较
<p>在 <code>Manim</code> 库中,<code>FunctionGraph</code>、<code>ImplicitFunction</code> 和 <code>ParametricFunction</code> 都是用于绘制函数图像的类,但它们的适用场景、输入形式和实现方式有显著区别。</p><p>以下是详细对比:</p>
<h1 id="1-functiongraph">1. FunctionGraph</h1>
<ul>
<li><strong>用途</strong>:绘制 <strong>显式函数</strong> ($ y = f(x) $) 的图像(单值函数)。</li>
<li><strong>输入要求</strong>:<br>
接受一个 <strong>一元函数</strong> ($ f(x) <span class="math inline">\() 和 (\)</span> x $) 的范围(如<code>x_range=[-2, 2]</code>)。</li>
<li><strong>特点</strong>:
<ul>
<li>直接映射 ($ x \to (x, f(x)) $)。</li>
<li>要求函数是 <strong>单值</strong> 的(一个$ x <span class="math inline">\(对应唯一\)</span> y $)。</li>
</ul>
</li>
<li><strong>示例代码</strong>:</li>
</ul>
<pre><code class="language-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)
</code></pre>
<p><img src="https://img2024.cnblogs.com/blog/83005/202506/83005-20250605113912574-1075169239.png" alt="" loading="lazy"></p>
<h1 id="2-implicitfunction">2. ImplicitFunction</h1>
<ul>
<li><strong>用途</strong>:绘制 <strong>隐函数</strong> ($ F(x, y) = 0 $) 的图像(如圆、椭圆)。</li>
<li><strong>输入要求</strong>:<br>
接受一个 <strong>二元函数</strong> ($ F(x, y) <span class="math inline">\() 和 (\)</span> x, y $) 的范围(如<code>x_range=[-3, 3], y_range=[-3, 3]</code>)。</li>
<li><strong>特点</strong>:
<ul>
<li>通过数值方法(如 <code>Marching Squares</code> 算法)求解满足 ($ F(x, y) = 0 $) 的点集。</li>
<li>可绘制 <strong>多值曲线</strong>(如一个$ x <span class="math inline">\(对应多个\)</span>y ))。</li>
</ul>
</li>
<li><strong>示例代码</strong>:</li>
</ul>
<pre><code class="language-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)
</code></pre>
<p><img src="https://img2024.cnblogs.com/blog/83005/202506/83005-20250605113912568-1491834790.png" alt="" loading="lazy"></p>
<p><strong>笛卡尔叶形线</strong>(一种自交曲线),可以展示隐函数处理 <strong>多值曲线</strong> 的能力(一个<code>x</code>对应多个<code>y</code>)。</p>
<h1 id="3-parametricfunction">3. ParametricFunction</h1>
<ul>
<li><strong>用途</strong>:绘制 <strong>参数方程</strong> 定义的曲线(如螺旋线、摆线)。</li>
<li><strong>输入要求</strong>:<br>
接受一个 <strong>向量值函数</strong> ($ \mathbf{r}(t) = (x(t), y(t)) <span class="math inline">\() 和参数 (\)</span> t $) 的范围(如<code>t_range=</code>)。</li>
<li><strong>特点</strong>:
<ul>
<li>通过参数 ($ t $) 映射到点 $ (x(t), y(t)) $。</li>
<li>可绘制 <strong>任意参数化曲线</strong>(包括封闭曲线、自交曲线)。</li>
</ul>
</li>
<li><strong>示例代码</strong>:</li>
</ul>
<pre><code class="language-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=,# 完整周期
color=RED,
stroke_width=4,
)
self.add(rose_curve)
</code></pre>
<p><img src="https://img2024.cnblogs.com/blog/83005/202506/83005-20250605113912572-703326319.png" alt="" loading="lazy"></p>
<p><strong>四叶玫瑰线</strong>展示参数方程处理 <strong>封闭曲线</strong> 的能力,通过参数$ t $直接控制曲线生成过程。</p>
<h1 id="4-核心区别总结">4. 核心区别总结</h1>
<table>
<thead>
<tr>
<th>特性</th>
<th>FunctionGraph</th>
<th>ImplicitFunction</th>
<th>ParametricFunction</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>输入形式</strong></td>
<td>$ y = f(x) $</td>
<td>$ F(x, y) = 0 $</td>
<td>$ \mathbf{r}(t) = (x(t), y(t)) $</td>
</tr>
<tr>
<td><strong>函数类型</strong></td>
<td>显式函数(单值)</td>
<td>隐函数(多值)</td>
<td>参数方程</td>
</tr>
<tr>
<td><strong>适用场景</strong></td>
<td>简单函数(如 $ y = \sin x) $</td>
<td>复杂曲线(如椭圆、心形线)</td>
<td>任意参数化曲线(如螺旋线)</td>
</tr>
<tr>
<td><strong>计算复杂度</strong></td>
<td>低(直接计算)</td>
<td>高(数值求解)</td>
<td>中(采样计算)</td>
</tr>
<tr>
<td><strong>多值支持</strong></td>
<td>❌ 不支持</td>
<td>✔️ 支持</td>
<td>✔️ 支持</td>
</tr>
</tbody>
</table>
<h1 id="5-如何选择">5. 如何选择?</h1>
<ul>
<li>用 <code>FunctionGraph</code> 当函数能显式写成$ y = f(x) $时(如多项式、三角函数)。</li>
<li>用 <code>ImplicitFunction</code> 当函数以方程 $ F(x, y) = 0 $ 给出时(如圆、椭圆)。</li>
<li>用 <code>ParametricFunction</code> 当曲线用参数 ( <code>t</code> ) 描述时(如摆线、贝塞尔曲线)。</li>
</ul>
<p>通过理解这些差异,你可以根据函数的具体形式高效选择对应的 <code>Manim</code> 类进行绘制。</p><br><br>
来源:https://www.cnblogs.com/wang_yb/p/18912045
頁:
[1]