设为首页 加入收藏

TOP

【manim动画教程】-- 基本图形(一)
2023-07-23 13:43:48 】 浏览:31
Tags:manim

制作数学视频时,各类几何图形是使用最频繁的。
一般来说,常用的几何图形包括:线以及多边形

1. 点

是最简单图形,也是其他所有图形的基础。
绘制其他任何图形时,都是用来定位的。

manim中生成一个点很方便,只要给定一个坐标即可。
这里的坐标包含 [x, y, z]3个维度,如果绘制二维图形,将第三个坐标 z固定为 0

class DotSample(Scene):
    def construct(self):
        # 绘制 9个点
        for x in range(-1, 2):
            for y in range(1, -2, -1):
                p = Dot([x, y, 0])
                self.play(Create(p), run_time=0.5)

按照 3x3的格式绘制9个点

manim -p .\samples.py DotSample

out01.gif

2. 线

manim线其实都是线段,绘制线只要提供两个点的坐标。

2.1 直线

提供任意2个点,manim通过 Line来绘制线。

class LineSample(Scene):
    def construct(self):
        self._lines()

    def _lines(self):
        # 绘制 3 条线
        l = Line([-1, 1, 0], [1, 1, 0])
        self.play(Create(l), run_time=0.5)

        l = Line([-1, 0, 0], [1, 0, 0])
        self.play(Create(l), run_time=0.5)

        l = Line([-1, -1, 0], [1, -1, 0])
        self.play(Create(l), run_time=0.5)

运行效果:
out.gif

2.2 带箭头的线

绘制带箭头的线同样只要提供2个点。
只是绘制的对象不用 Line,而是用 Arrow

class LineSample(Scene):
    def construct(self):
        self._arrows()

    def _arrows(self):
        a = Arrow([-1, 1, 0], [1, 1, 0])
        self.play(Create(a), run_time=0.5)

        a = Arrow([-1, 0, 0], [1, 0, 0])
        self.play(Create(a), run_time=0.5)

        a = Arrow([-1, -1, 0], [1, -1, 0])
        self.play(Create(a), run_time=0.5)

运行效果:
out.gif

2.3 虚线

绘制虚线使用 DashedLine

class LineSample(Scene):
    def construct(self):
        self._dashedLines()
        self.wait()

    def _dashedLines(self):
        dl = DashedLine([-1, 1, 0], [1, 1, 0])
        self.play(Create(dl), run_time=0.5)

        dl = DashedLine([-1, 0, 0], [1, 0, 0])
        self.play(Create(dl), run_time=0.5)

        dl = DashedLine([-1, -1, 0], [1, -1, 0])
        self.play(Create(dl), run_time=0.5)

运行效果:
out.gif

3. 圆

绘制只要提供半径即可,圆心默认在屏幕的中心。
绘制圆使用 Circle

class CircleSample(Scene):
    def construct(self):
        self._circles()
        self.wait()

    def _circles(self):
        c = Circle(radius=1)
        self.play(Create(c), run_time=0.5)

        c = Circle(radius=2)
        self.play(Create(c), run_time=0.5)

        c = Circle(radius=3)
        self.play(Create(c), run_time=0.5)

运行效果:
out.gif

3.1 椭圆

manim也支持绘制椭圆,使用 Ellipse
绘制椭圆的两个参数 widthheight分别控制椭圆最大宽度和最大高度。

class CircleSample(Scene):
    def construct(self):
        self._ellipses()
        self.wait()

    def _ellipses(self):
        e = Ellipse(width=1, height=0.5)
        self.play(Create(e), run_time=0.5)

        e = Ellipse(width=2, height=1)
        self.play(Create(e), run_time=0.5)

        e = Ellipse(width=3, height=1.5)
        self.play(Create(e), run_time=0.5)

运行效果:
out.gif

3.2 圆弧

manim中绘制圆弧主要有三个参数:

  1. angle:圆弧的弧度
  2. start_angle: 开始的角度,默认 0
  3. radius:圆弧的半径
class CircleSample(Scene):
    def construct(self):
        self._arcs()
        self.wait()

    def _arcs(self):
        # 90度圆弧,半径1
        a = Arc(angle=PI / 2, radius=1)
        self.play(Create(a), run_time=0.5)

        # 180度圆弧,半径2
        a = Arc(angle=PI, radius=2)
        self.play(Create(a), run_time=0.5)

        # 30度圆弧,半径2,从270度开始绘制
        a = Arc(angle=PI / 6, start_angle=PI * 1.5, radius=2)
        self.play(Create(a), run_time=0.5)

运行效果:
out.gif

4. 多边形

如果制作几何相关的数学视频,那么多边形绝对是使用最多的。
manim对多边形的支持非常完善,常用的主要以下几种:

4.1 等边三角形

manim中专门有绘制等边三角形的对象 Triangle

class PolygonSample(Scene):
    def construct(self):
        self._triangles()
        self.wait()

    def _triangles(self):
        t = Triangle()
        self.play(Create(t))

运行效果:
out.gif

4.2 四边形

四边形比三角形应用的更广,所以 manim也提供了更多绘制四边形的方法。

4.2.1 正方形

绘制正方形主要有一个参数,就是 side_length(正方形的边长)。

class PolygonSample(Scene):
    def construct(self):
        self._squares()
        self.wait()

    def _squares(self):
        s = Square(side_length=1)
        self.play(Create(s), run_time=0.5)

        s = Square(side_length=1.5)
        self.play(Create(s), run_time=0.5)

        s = Square(side_length=2)
        self.play(Creat
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇【0基础学爬虫】爬虫基础之网页解.. 下一篇python中函数的返回值详解

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目