[manim教程]2.基本图形 您所在的位置:网站首页 多边形求边长 [manim教程]2.基本图形

[manim教程]2.基本图形

2023-06-16 10:10| 来源: 网络整理| 查看: 265

常用的几何图形包括:点、线、圆及多边形.

一、点

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)

按照3*3的格式绘制9个点.

manim -p .\samples.py DotSample

Alt

二、线

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)

Alt

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)

Alt

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)

Alt

三、圆

绘制圆只要提供半径即可,圆心默认在屏幕的中心. 绘制圆使用 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)

Alt

3.1 椭圆

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

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)

Alt

3.2 圆弧

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

angle 弧度;start_angle 开始的角度,默认 0 ;radius 半径. 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)

Alt

四、多边形 4.1 等边三角形

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

class PolygonSample(Scene): def construct(self): self._triangles() self.wait() def _triangles(self): t = Triangle() self.play(Create(t))

Alt

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(Create(s), run_time=0.5)

Alt (2)矩形:绘制矩形有两个主要的参数,分别代表矩形的高 height 和宽 width .

class PolygonSample(Scene): def construct(self): self._rectangles() self.wait() def _rectangles(self): r = Rectangle(width=1.5, height=1) self.play(Create(r), run_time=0.5) r = Rectangle(width=2, height=1.5) self.play(Create(r), run_time=0.5) r = Rectangle(width=3, height=2) self.play(Create(r), run_time=0.5)

Alt (3)圆角矩形:圆角矩形和矩形相比,多了一个参数 corner_radius 用来控制矩形四个角的弧度半径.

class PolygonSample(Scene): def construct(self): self._rounded_rectangles() self.wait() def _rounded_rectangles(self): r = RoundedRectangle(corner_radius=0.2, width=1.5, height=1) self.play(Create(r), run_time=0.5) r = RoundedRectangle(corner_radius=0.4, width=2, height=1.5) self.play(Create(r), run_time=0.5) r = RoundedRectangle(corner_radius=0.6, width=3, height=2) self.play(Create(r), run_time=0.5)

Alt

4.3 任意多边形

除了三角形和四边形,manim还提供了一个通用的 Polygon 对象,它会依次连接传入的坐标点列表,绘制任意多边形.

class PolygonSample(Scene): def construct(self): self._polygons() self.wait() def _polygons(self): p = Polygon([-3, 1, 0], [-1, 1, 0], [-2, -1, 0]) self.play(Create(p), run_time=0.5) p = Polygon([1, 1, 0], [2, 0, 0], [3, 1, 0], [3, -1, 0], [1, -1, 0]) self.play(Create(p), run_time=0.5)

Alt

4.4 正多边形

虽然 manim提供了绘制任意多边形的对象 Polygon ,利用 Polygon 绘制正多边形理论上是完全可行的. 不过,要自己去计算各个正多边形的坐标点显然有些费时费力, 所以,manim中还提供了一个专门用来绘制正多边形的对象 RegularPolygon .

class PolygonSample(Scene): def construct(self): self._reguler_polygons() self.wait() def _reguler_polygons(self): p1 = RegularPolygon(n=6) # 正六边形 p2 = RegularPolygon(n=8) # 正八边形 p3 = RegularPolygon(n=10) # 正十边形 vg = VGroup(p1, p2, p3) vg.arrange(RIGHT, buff=SMALL_BUFF) self.play(Create(vg))

Alt



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有