manim
manim
是用于解释性数学视频的动画引擎。它用于以编程方式创建精确的动画。
如果我们需要一个动画来解释一个数学现象,或者用于教学的话,manim
无疑是一个很好的选择。
manim
可以清楚的用动画的特性解释,数学公式的过程,让数学生动起来,更好的理解数学公式的意义。
manim
安装和使用也比较简单。如下:
安装
ubuntu
下的安装:
首先安装Cairo
, Pango
, 和FFmpeg
,命令如下:
sudo apt update
sudo apt install libcairo2-dev libpango1.0-dev ffmpeg
然后,继续安装manim
,命令如下:
pip3 install manim -i https://pypi.tuna.tsinghua.edu.cn/simple
这个时候如果出现安装失败的情况,请检查自己的python
版本和ubuntu
版本,最好使用的都是最新的版本。
代码示例
安装完成了 manim
,我们当然迫不及待的想看看 manim
能给我们带来什么,先不要急,还得从hello world
。
构建一个场景,一个正方形变成了一个圆形。
代码如下:
from manim import *
class SquareToCircle(Scene):
def construct(self):
circle = Circle()
square = Square()
square.flip(RIGHT)
square.rotate(-3 * TAU / 8)
circle.set_fill(PINK, opacity=0.5)
self.play(Create(square))
self.play(Transform(square, circle))
self.play(FadeOut(square))
代码也直接简单,放到文件 ex.py
下,Transform
函数,让正方形过度到圆形。
运行后,输出如下:
root@lqp:/home/lqp/project# manim -p -ql ex.py SquareToCircle
Manim Community v0.16.0.post0
[07/29/22 11:10:14] INFO Animation 0 : Partial movie file written in scene_file_writer.py:514
'/home/lqp/project/media/videos/ex/480p15/partial_movie_files/SquareToCircle/3163782288_2330473256_223132457.mp4'
INFO Animation 1 : Partial movie file written in scene_file_writer.py:514
'/home/lqp/project/media/videos/ex/480p15/partial_movie_files/SquareToCircle/2201830969_2769193253_2319886907.mp4'
INFO Animation 2 : Partial movie file written in scene_file_writer.py:514
'/home/lqp/project/media/videos/ex/480p15/partial_movie_files/SquareToCircle/2201830969_4065880486_2319886907.mp4'
INFO Combining to Movie file. scene_file_writer.py:607
[07/29/22 11:10:15] INFO scene_file_writer.py:728
File ready at '/home/lqp/project/media/videos/ex/480p15/SquareToCircle.mp4'
INFO Rendered SquareToCircle scene.py:240
Played 3 animations
INFO Previewed File at: '/home/lqp/project/media/videos/ex/480p15/SquareToCircle.mp4' file_ops.py:224
Error: no "view" mailcap rules found for type "video/mp4"
/usr/bin/xdg-open: 882: www-browser: not found
/usr/bin/xdg-open: 882: links2: not found
/usr/bin/xdg-open: 882: elinks: not found
/usr/bin/xdg-open: 882: links: not found
/usr/bin/xdg-open: 882: lynx: not found
/usr/bin/xdg-open: 882: w3m: not found
xdg-open: no method available for opening '/home/lqp/project/media/videos/ex/480p15/SquareToCircle.mp4'
有一些提示找不到的东西,我们不用管它,这是找不到播放的软件。
播放生成的mp4
。
在看一个比较复杂的实例,用圆的滚动,生成正弦。
因为我们用到了标签,标签使用的是LaTeX
,所以需要安装下LaTeX
:
sudo apt-get install texlive-full
texlive-full
比较大,需要花一段的时间。
代码如下:
from manim import *
class SineCurveUnitCircle(Scene):
# contributed by heejin_park, https://infograph.tistory.com/230
def construct(self):
self.show_axis()
self.show_circle()
self.move_dot_and_draw_curve()
self.wait()
def show_axis(self):
x_start = np.array([-6,0,0])
x_end = np.array([6,0,0])
y_start = np.array([-4,-2,0])
y_end = np.array([-4,2,0])
x_axis = Line(x_start, x_end)
y_axis = Line(y_start, y_end)
self.add(x_axis, y_axis)
self.origin_point = np.array([-4,0,0])
self.curve_start = np.array([-3,0,0])
def show_circle(self):
circle = Circle(radius=1)
circle.move_to(self.origin_point)
self.add(circle)
self.circle = circle
def move_dot_and_draw_curve(self):
orbit = self.circle
origin_point = self.origin_point
dot = Dot(radius=0.08, color=YELLOW)
dot.move_to(orbit.point_from_proportion(0))
self.t_offset = 0
rate = 0.25
def go_around_circle(mob, dt):
self.t_offset += (dt * rate)
# print(self.t_offset)
mob.move_to(orbit.point_from_proportion(self.t_offset % 1))
def get_line_to_circle():
return Line(origin_point, dot.get_center(), color=BLUE)
def get_line_to_curve():
x = self.curve_start[0] + self.t_offset * 4
y = dot.get_center()[1]
return Line(dot.get_center(), np.array([x,y,0]), color=YELLOW_A, stroke_width=2 )
self.curve = VGroup()
self.curve.add(Line(self.curve_start,self.curve_start))
def get_curve():
last_line = self.curve[-1]
x = self.curve_start[0] + self.t_offset * 4
y = dot.get_center()[1]
new_line = Line(last_line.get_end(),np.array([x,y,0]), color=YELLOW_D)
self.curve.add(new_line)
return self.curve
dot.add_updater(go_around_circle)
origin_to_circle_line = always_redraw(get_line_to_circle)
dot_to_curve_line = always_redraw(get_line_to_curve)
sine_curve_line = always_redraw(get_curve)
self.add(dot)
self.add(orbit, origin_to_circle_line, dot_to_curve_line, sine_curve_line)
self.wait(8.5)
dot.remove_updater(go_around_circle)
本文含有隐藏内容,请 开通VIP 后查看