Github每日精选(第25期):数学动画引擎manim

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)

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
io.github.tencent:vap 是一个基于 Java 的验证框架,它并不与动画播放有任何关系。因此,不能使用 io.github.tencent:vap 来实现动画播放。 如果您想要实现动画播放,可以考虑使用 Android 中的动画 API,例如属性动画、帧动画、转场动画等。这些 API 提供了丰富的动画效果和控制方式,可以满足不同场景下的需求。 例如,在使用属性动画时,可以使用 ObjectAnimator 类来实现属性变化的动画效果。例如,可以将一个 View 对象的 alpha 属性从 0 变化到 1,实现一个淡入效果: ``` ObjectAnimator animator = ObjectAnimator.ofFloat(view, "alpha", 0f, 1f); animator.setDuration(1000); animator.start(); ``` 在使用帧动画时,可以将一组连续的图片资源合成一张大图,并通过 XML 文件来指定播放顺序和播放速度。例如,可以创建一个名为 anim_fade.xml 的文件来实现一个淡入淡出的帧动画: ``` <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/fade_in" android:duration="1000"/> <item android:drawable="@drawable/fade_out" android:duration="1000"/> </animation-list> ``` 然后在 Java 代码中使用 AnimationDrawable 类来加载和播放该动画: ``` ImageView imageView = findViewById(R.id.image_view); AnimationDrawable animationDrawable = (AnimationDrawable) getResources().getDrawable(R.drawable.anim_fade); imageView.setImageDrawable(animationDrawable); animationDrawable.start(); ``` 总的来说,Android 中提供了多种动画 API,可以方便地实现各种动画效果。您可以根据具体需求选择合适的 API 来实现动画播放。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

go2coding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值