【Manim CE】常用Mobject與使用

2022-08-28 18:01:19

當前檔案版本:v0.16.0.post0

第一部分:向量化父類別VMobject

本文所有物體都繼承自此,故很多引數只在本部分闡述。


VMobject繼承自Mobject

V的意思是向量化的,vectorized mobject

fill_color=None,
fill_opacity=0.0,
stroke_color=None,
stroke_opacity=1.0,
stroke_width=DEFAULT_STROKE_WIDTH,
background_stroke_color=BLACK,
background_stroke_opacity=1.0,
background_stroke_width=0,
sheen_factor=0.0,
sheen_direction=UL,
close_new_points=False,
pre_function_handle_to_anchor_scale_factor=0.01,
make_smooth_after_applying_functions=False,
background_image=None,
shade_in_3d=False,
tolerance_for_point_equality=1e-6,
n_points_per_cubic_curve=4,
**kwargs

構造引數:

  • fill_color 填充顏色
  • fill_opacity 填充透明度
  • stroke_color 邊框顏色
  • stroke_opacity 邊框顏色透明度
  • stroke_width 邊框寬度
    • 注:DEFAULT_STROKE_WIDTH = 4
  • background_stroke_color
  • background_stroke_opacity
  • background_stroke_width
  • sheen_factor 物體的光澤
  • sheen_direction 物體光澤的中心
  • close_new_points Indicates that it will not be displayed, but that it should count in parent mobject’s path
  • ...

 

第一節:幾何

本節列舉大量常用的幾何圖形。

manim.mobject.geometry


Circle 圓

manim.mobject.geometry.arc.Circle

radius: float | None = None,
color: Color | str = RED,
**kwargs,

構造引數:

  • radius(float或None)圓的半徑,例如 1
  • color(str或Color)圓形的顏色,例如 WHITE
  • **kwargs 附加引數

構造範例:

from manim import *

class CircleExample(Scene):
    def construct(self):
        circle_1 = Circle(radius=1.0)
        circle_2 = Circle(radius=1.5, color=GREEN)
        circle_3 = Circle(radius=1.0, color=BLUE_B, fill_opacity=1)

        circle_group = Group(circle_1, circle_2, circle_3).arrange(buff=1)
        self.add(circle_group)

 

Dot 點

manim.mobject.geometry.arc.Dot

point: list | np.ndarray = ORIGIN,
radius: float = DEFAULT_DOT_RADIUS,
stroke_width: float = 0,
fill_opacity: float = 1.0,
color: Color | str = WHITE,

構造引數:

  • point(陣列)螢幕座標,例如 [0,0,0]
  • radius(float)點的半徑,例如 0.05
  • stroke_width (float)點的輪廓寬度,例如 0.01
  • fill_opacity(float)點內部的顏色,例如 YELLOW
  • **kwargs 附加引數

構造範例:

from manim import *

class DotExample(Scene):
    def construct(self):
        dot1 = Dot(point=LEFT, radius=0.08)
        dot2 = Dot(point=ORIGIN)
        dot3 = Dot(point=RIGHT)
        self.add(dot1,dot2,dot3)

 

Ellipse 橢圓

manim.mobject.geometry.arc.Ellipse

width: float = 2,
height: float = 1,
**kwargs

 構造引數:

  • width(float)短軸
  • height(float)長軸
  • **kwargs 附加引數

 構造範例:

from manim import *

class EllipseExample(Scene):
    def construct(self):
        ellipse_1 = Ellipse(width=2.0, height=4.0, color=BLUE_B)
        ellipse_2 = Ellipse(width=4.0, height=1.0, color=BLUE_D)
        ellipse_group = Group(ellipse_1,ellipse_2).arrange(buff=1)
        self.add(ellipse_group)

 

Angle 角

manim.mobject.geometry.line.Angle 

line1: Line,
line2: Line,
radius: float = None,
quadrant=(1, 1),
other_angle: bool = False,
dot=False,
dot_radius=None,
dot_distance=0.55,
dot_color=WHITE,
elbow=False,
**kwargs,

構造引數:

  • line1(Line)起始線
    • 注:Line可以視為兩個點座標的集合,兩點連成的線段。
  • line2(Line)終止線
    • 注:line1、line2兩條線不能平行
  • radius(float)原點與角度弧線的距離半徑
  • quadrant 角度弧線的象限,可傳入:(1,1) (1,-1) (-1,1) (-1,-1)
  • other_angle(bool)從正方向畫角度弧線
  • dot(bool)弧度中心標記一個點,一般用於表示角度位置
  • dot_radius(float)點的半徑
  • dot_distance(float)點離原點的距離
  • elbow(bool)表示直角的角度折現
  • **kwargs 附加引數

構造範例:

from manim import *

class RightArcAngleExample(Scene):
    def construct(self):
        line1 = Line( LEFT, RIGHT )
        line2 = Line( DOWN, UP )
        rightarcangles = [
            Angle(line1, line2, dot=True),
            Angle(line1, line2, radius=0.4, quadrant=(1,-1), dot=True, other_angle=False),
            Angle(line1, line2, radius=0.5, quadrant=(-1,1), stroke_width=8, dot=True, dot_color=YELLOW, dot_radius=0.04, other_angle=True),
            Angle(line1, line2, radius=0.7, quadrant=(-1,-1), color=RED, dot=True, dot_color=GREEN, dot_radius=0.08),
        ]
        plots = VGroup()
        for angle in rightarcangles:
            plot=VGroup(line1.copy(),line2.copy(), angle)
            plots.add(plot)
        plots.arrange(buff=1.5)
        self.add(plots)

 

 

Line 線

manim.mobject.geometry.line.Line

start=LEFT,
end=RIGHT,
buff=0,
path_arc=None,
**kwargs

構造引數:

  • start(list)起始點
  • end(list)終點
  • buff(float)兩端點與可見線的距離
  • **kwargs 附加引數

構造範例:

from manim import *

class LineExample(Scene):
    def construct(self):
        ax = Axes()
        line1 = Line(ax.c2p(1,-3),ax.c2p(1,3),buff=0)
        line2 = Line(ax.c2p(2,-3),ax.c2p(2,3),buff=1)
        line3 = Line(ax.c2p(2,-3),ax.c2p(2,3),path_arc=PI)
        self.add(ax,line1,line2,line3)

 

Rectangle 矩形

manim.mobject.geometry.polygram.Rectangle

color: Color = WHITE,
height: float = 2.0,
width: float = 4.0,
grid_xstep: float | None = None,
grid_ystep: float | None = None,
**kwargs,

構造引數:

  • color(Color)顏色
  • height(float)高度
  • width(float)寬度
  • grid_xstep(float或None)分割x
  • gird_ystep(float或None)分割y
  • **kwargs 附加引數

構造範例:

from manim import *

class RectangleExample(Scene):
    def construct(self):
        rect1 = Rectangle(width=4.0, height=2.0, grid_xstep=1.0, grid_ystep=0.5)
        rect2 = Rectangle(width=1.0, height=4.0)

        rects = Group(rect1,rect2).arrange(buff=1)
        self.add(rects)

 

Square 正方形

manim.mobject.geometry.polygram.Square

side_length=2.0,
**kwargs

構造引數:

  • side_length (float)邊長
  • **kwargs 構造引數

構造範例:

from manim import *

class SquareExample(Scene):
    def construct(self):
        square = Square(side_length=2.0,
                     color=BLUE,fill_opacity=1,sheen_factor=0.8)
        self.add(square)

 

 

Star 星

 manim.mobject.geometry.polygram.Star

n: int = 5,
*,
outer_radius: float = 1,
inner_radius: float | None = None,
density: int = 2,
start_angle: float | None = TAU / 4,
**kwargs,

 構造引數:

  • n(int)節點數量
  • outer_radius(float)外半徑
  • inner_radius(float)內半徑
  • density()

 構造範例:

from manim import *

class StarExample(Scene):
    def construct(self):
        pentagram = RegularPolygram(5, radius=2).to_edge(LEFT)
        star = Star(outer_radius=2, color=RED).next_to(pentagram,RIGHT)
        sum = VGroup(
            pentagram.copy().to_edge(RIGHT),
            star.copy().to_edge(RIGHT))
        self.add(pentagram,star,sum)

from manim import *

class StarExample(Scene):
    def construct(self):
        pentagram = RegularPolygram(7, radius=2).to_edge(LEFT)
        star = Star(n=7,outer_radius=2, color=RED).next_to(pentagram,RIGHT)
        sum = VGroup(
            pentagram.copy().to_edge(RIGHT),
            star.copy().to_edge(RIGHT))
        self.add(pentagram,star,sum)

 

 Triangle 三角形

manim.mobject.geometry.polygram.Triangle 

構造引數:

 構造範例:

from manim import *

class TriangleExample(Scene):
    def construct(self):
        triangle_1 = Triangle()
        triangle_2 = Triangle().scale(2).rotate(60*DEGREES)
        tri_group = Group(triangle_1, triangle_2).arrange(buff=1)
        self.add(tri_group)

 

SurroundingRectangle 環繞矩形

manim.mobject.geometry.shape\_matchers.SurroundingRectangle

mobject,
color=YELLOW,
buff=SMALL_BUFF,
corner_radius=0.0,
**kwargs

構造引數:

  • mobject(Mobject)被環繞的物體
  • buff(float)邊框與物體的距離
  • corner_radius(float)邊框圓角半徑

構造範例:

from manim import *

class SurroundingRectExample(Scene):
    def construct(self):
        title = Title("Fourier Transform")
        quote = Text('''
Generally speaking, the two ways to describe the same problem 
can be called that they are the mutual transform to each other.
''',color=BLUE,
        ).scale(0.75)
        box = SurroundingRectangle(quote, color=YELLOW, buff=MED_LARGE_BUFF)

        t2 = Tex(r"Hello World").scale(1.5)
        box2 = SurroundingRectangle(t2, corner_radius=0.2)
        mobjects = VGroup(VGroup(box, quote), VGroup(t2, box2)).arrange(DOWN)
        self.add(title, mobjects)

 

Underline 下劃線

manim.mobject.geometry.shape\_matchers.Underline

mobject,
buff=SMALL_BUFF,
**kwargs

構造引數:

  • mobject(Mobject)被標記下劃線的物體
  • buff(float)間距

構造範例:

from manim import *

class UnderLine(Scene):
    def construct(self):
        man = MathTex("Manim \enspace Remoo")  # Full Word
        ul = Underline(man)  # Underlining the word
        self.add(man, ul)

 

 

第二節:數學影象

本節主要儘可能詳細的列舉需要使用的數學影象VMobject。

manim.mobject.graphing


 Axes 直角座標

manim.mobject.graphing.coordinate\_systems.Axes

x_range: Sequence[float] | None = None,
y_range: Sequence[float] | None = None,
x_length: float | None = round(config.frame_width) - 2,
y_length: float | None = round(config.frame_height) - 2,
axis_config: dict | None = None,
x_axis_config: dict | None = None,
y_axis_config: dict | None = None,
tips: bool = True,
**kwargs,

構造引數:

  • x_range(Sequence[float])座標的橫座標範圍,序列  [左邊界,右邊界,步進]
  • y_range(Sequence[float])座標的縱座標範圍,序列  [下邊界,上邊界,步進]
  • x_length(float)座標物體Mobject的寬度
  • y_length(float)座標物體Mobject的高度
  • axis_config(dict)座標引數集
  • x_axis_config 同上
  • y_axis_config 同上
  • tips(bool)座標箭頭

構造範例:

from manim import *

class AxesExample(Scene):
    def construct(self):
        ax1 = Axes(
            x_range=[-2*2*PI,2*2*PI,PI],
            y_range=[-1,1,0.5],
            x_length=6,
            tips=True).to_edge(LEFT)
        graph1 = ax1.plot(lambda x : np.sin(x))
        ax2 = Axes(
            x_range=[-1,4,1],
            y_range=[-2,2,1],
            x_length=6,
            tips=False).to_edge(RIGHT)
        graph2 = VGroup(*(Dot(ax2.c2p(x-1,2-x)) for x in range(5)))

        self.add(ax1,graph1,ax2,graph2)

from manim import *

class LogScalingExample(Scene):
    def construct(self):
        ax = Axes(
            x_range=[0, 10, 1],
            y_range=[-2, 6, 1],
            tips=False,
            axis_config={"include_numbers": True},
            y_axis_config={"scaling": LogBase(custom_labels=True)},
        )

        graph = ax.plot(lambda x: x ** 2, x_range=[0.001, 10,0.01])
        self.add(ax, graph)

 

NumberPlane 數軸