Manim绘制math.sqrt(1-x**2)图形不光滑有凸起的解决方法
解决Manim绘制上半圆曲线时的不自然凸起问题
我是Manim新手,在绘制math.sqrt(1-x**2)(上半圆)的图形时,发现图形存在不自然的凸起,以下是我的代码:
from manim import * import math class Myscene(Scene): def construct(self): axes = Axes(x_range=[-2.2, 2.2], y_range=[-1.1, 1.1] ) axes.add_coordinates() self.play(Write(axes)) self.wait(1) graph = axes.plot(lambda x: math.sqrt(1-x**2), x_range=[-1, 1], color = RED, ) self.play(Write(graph), run_time = 5) self.wait(5)
问题原因
出现凸起是因为Manim默认的采样点数不足,无法精确还原上半圆这种在x趋近于±1时导数急剧变化的曲线,稀疏采样点会导致折线化的凸起。
解决方法
通过以下两种方式优化即可:
- 增加采样点数:在
plot()方法中设置num_samples参数,提升采样密度 - 开启平滑处理:配合
use_smoothing=True对采样点进行插值平滑,进一步优化曲线形态
修改后的代码示例
from manim import * import math class Myscene(Scene): def construct(self): axes = Axes(x_range=[-2.2, 2.2], y_range=[-1.1, 1.1] ) axes.add_coordinates() self.play(Write(axes)) self.wait(1) # 方法1:仅增加采样点数,推荐设置200以上 graph = axes.plot(lambda x: math.sqrt(1-x**2), x_range=[-1, 1], color = RED, num_samples=200 ) # 方法2:开启平滑+适度采样点,也能达到良好效果 # graph = axes.plot(lambda x: math.sqrt(1-x**2), # x_range=[-1, 1], # color = RED, # use_smoothing=True, # num_samples=150 # ) self.play(Write(graph), run_time = 5) self.wait(5)
内容的提问来源于stack exchange,提问作者HelloMoto
相关产品推荐
相关产品推荐

