欢迎关注《前端点线面》,开车不迷路。
今天要介绍的是一款免费开源的数学动画引擎——Manim,其是一个优秀的创建数学动画的Python库,让数学变的更具有可视化,一反面降低理解成本,另一方面可以制作一些炫酷的教案。
关键特性
支持各种数学对象的动画演示
你可以创建和操作多种数学对象,例如数字、字母、函数图像、几何形状、三维对象等,为数学概念和理论提供直观的演示。
强大的动画表达能力
Manim 支持各种动画效果,如淡入、淡出、旋转、缩放、颜色渐变、路径跟踪等。这些效果可单独使用,也可组合使用以创建复杂的动画。
支持 LaTeX 和 Markdown
可以使用 LaTeX 来创建复杂的数学公式。
Python 编程语言支持
Manim 用 Python 编写,有极好的可定制性。你可以使用 Python 的丰富库和框架,如 NumPy 和 Matplotlib,来扩展 Manim 的功能。
支持 2D 和 3D 动画
你可以在 2D 平面或 3D 空间中创建和操控对象。对于 3D 动画,Manim 提供了许多实用的功能,例如摄像机控制、光照和阴影等。
开源和跨平台
Manim 是一个开源项目,你可以自由使用和修改它。并且,Manim 可以在主流的操作系统中运行,如 Windows、macOS 和 Linux。
炫酷demo
其官网提供了很多炫酷的demo,地址为https://docs.manim.org.cn/getting_started/example_scenes.html#textransformexample,下面简要看两个:
简单变化
创建一个正方形,并将该正方形变换为圆形
# 引入在使用manim时所有可能会用到的类
from manimlib import *
# 创建一个Scene的子类
class SquareToCircle(Scene):
def construct(self):
circle = Circle()
circle.set_fill(BLUE, opacity=0.5)
circle.set_stroke(BLUE, width=4)
square = Square()
self.play(ShowCreation(square))
self.wait()
self.play(ReplacementTransform(square, circle))
self.wait()
函数图像
创建了一个彩色的图像,被用来描述几个不同的函数:
sin(x)
、ReLU(Rectified Linear Unit)函数,然后描述一个阶梯函数(Step function),最后描述一个四次函数(0.25*x^2
)。
from manimlib import *
class GraphExample(Scene):
def construct(self):
axes = Axes((-3, 10), (-1, 8))
axes.add_coordinate_labels()
self.play(Write(axes, lag_ratio=0.01, run_time=1))
# Axes.get_graph会返回传入方程的图像
sin_graph = axes.get_graph(
lambda x: 2 * math.sin(x),
color=BLUE,
)
# 默认情况下,它在所有采样点(x, f(x))之间稍微平滑地插值
# 但是,如果图形有棱角,可以将use_smoothing设为False
relu_graph = axes.get_graph(
lambda x: max(x, 0),
use_smoothing=False,
color=YELLOW,
)
# 对于不连续的函数,你可以指定间断点来让它不试图填补不连续的位置
step_graph = axes.get_graph(
lambda x: 2.0 if x > 3 else 1.0,
discontinuities=[3],
color=GREEN,
)
# Axes.get_graph_label可以接受字符串或者mobject。如果传入的是字符串
# 那么将将其当作LaTeX表达式传入Tex中
# 默认下,label将生成在图像的右侧,并且匹配图像的颜色
sin_label = axes.get_graph_label(sin_graph, "\\sin(x)")
relu_label = axes.get_graph_label(relu_graph, Text("ReLU"))
step_label = axes.get_graph_label(step_graph, Text("Step"), x=4)
self.play(
ShowCreation(sin_graph),
FadeIn(sin_label, RIGHT),
)
self.wait(2)
self.play(
ReplacementTransform(sin_graph, relu_graph),
FadeTransform(sin_label, relu_label),
)
self.wait()
self.play(
ReplacementTransform(relu_graph, step_graph),
FadeTransform(relu_label, step_label),
)
self.wait()
parabola = axes.get_graph(lambda x: 0.25 * x**2)
parabola.set_stroke(BLUE)
self.play(
FadeOut(step_graph),
FadeOut(step_label),
ShowCreation(parabola)
)
self.wait()
# 你可以使用Axes.input_to_graph_point(缩写Axes.i2gp)来找到图像上的一个点
dot = Dot(color=RED)
dot.move_to(axes.i2gp(2, parabola))
self.play(FadeIn(dot, scale=0.5))
# ValueTracker存储一个数值,可以帮助我们制作可变参数的动画
# 通常使用updater或者f_always让其它mobject根据其中的数值来更新
x_tracker = ValueTracker(2)
f_always(
dot.move_to,
lambda: axes.i2gp(x_tracker.get_value(), parabola)
)
self.play(x_tracker.animate.set_value(4), run_time=3)
self.play(x_tracker.animate.set_value(-2), run_time=3)
self.wait()
github及官网地址
github地址
https://github.com/3b1b/manim
官网地址
https://docs.manim.org.cn/getting_started/installation.html
茶已备好,只待君来!感谢关注 前端点线面 (>‿<),本号定期推荐原创深度好文,帮助每一位在前端领域打拼的伙伴们走向前列。此外,《工具人的工具箱》小程序也上线了,内含11个模块338道面试题,不容错过!!!
工具人的工具箱