[ PROMPT_NODE_27756 ]
mobjects
[ SKILL_DOCUMENTATION ]
# Manim 中的 Mobjects
Mobjects(数学对象)是 Manim 动画的构建块。它们代表了屏幕上可以显示的任何内容。
## 基本形状
python
from manim import *
class BasicShapes(Scene):
def construct(self):
# 圆形
circle = Circle(radius=1, color=BLUE)
circle.set_fill(BLUE, opacity=0.5)
# 正方形
square = Square(side_length=2, color=RED)
# 矩形
rectangle = Rectangle(width=3, height=1.5, color=GREEN)
# 显示它们
self.play(Create(circle))
self.play(Create(square))
self.play(Create(rectangle))
## 常用 Mobjects
### 几何形状
python
class GeometricShapes(Scene):
def construct(self):
# 基本形状
circle = Circle()
square = Square()
triangle = Triangle()
pentagon = RegularPolygon(n=5)
# 排成一行
shapes = VGroup(circle, square, triangle, pentagon)
shapes.arrange(RIGHT, buff=0.5)
self.play(Create(shapes))
### 线条与箭头
python
class LinesAndArrows(Scene):
def construct(self):
# 线条
line = Line(start=LEFT, end=RIGHT, color=BLUE)
# 箭头
arrow = Arrow(start=LEFT, end=RIGHT, color=RED)
# 双向箭头
double_arrow = DoubleArrow(start=LEFT * 2, end=RIGHT * 2)
# 向量
vector = Vector(direction=UP + RIGHT)
arrows = VGroup(line, arrow, double_arrow, vector)
arrows.arrange(DOWN, buff=0.5)
self.play(Create(arrows))
## Mobject 属性
### 颜色与透明度
python
class ColorOpacity(Scene):
def construct(self):
circle = Circle()
# 设置描边颜色
circle.set_stroke(color=BLUE, width=5)
# 设置填充
circle.set_fill(RED, opacity=0.7)
self.play(Create(circle))
### 大小与位置
python
class SizePosition(Scene):
def construct(self):
square = Square()
# 缩放
square.scale(2)
# 移动
square.shift(UP * 2 + RIGHT * 3)
# 旋转
square.rotate(PI / 4)
self.play(Create(square))
## 文本与 LaTeX
python
class TextExample(Scene):
def construct(self):
# 普通文本
text = Text("Hello, Manim!")
# LaTeX 文本
formula = MathTex(r"e^{ipi} + 1 = 0")
# 方程
equation = Tex(r"The famous equation: $E = mc^2$")
group