给我几个manim的代码
时间: 2023-05-27 13:01:46 浏览: 54
以下是一些manim的代码示例:
1. 绘制简单的图形
```python
from manim import *
class MyFirstScene(Scene):
def construct(self):
circle = Circle()
self.play(Create(circle))
self.wait()
```
2. 绘制LaTeX公式
```python
from manim import *
class MyFirstScene(Scene):
def construct(self):
tex = Tex(r"\sum_{i=1}^n i^2 = \frac{n(n+1)(2n+1)}{6}")
self.play(Create(tex))
self.wait()
```
3. 绘制图表
```python
from manim import *
import numpy as np
class MyFirstScene(Scene):
def construct(self):
x_min, x_max = -5, 5
y_min, y_max = -2, 2
x_axis = NumberLine(x_range=[x_min, x_max], length=10, decimals=1)
y_axis = NumberLine(y_range=[y_min, y_max], length=4, decimals=1).shift(3*DOWN)
graph = FunctionGraph(lambda x: np.sin(x), x_range=[x_min, x_max], color=BLUE)
self.add(x_axis, y_axis, graph)
self.wait()
```
这将绘制出一个sin函数的图表。
4. 创建动画效果
```python
from manim import *
class MyFirstScene(Scene):
def construct(self):
circle = Circle()
square = Square()
self.play(Create(circle))
self.play(Transform(circle, square))
self.wait()
```
这将绘制出一个圆形,然后将其转换为正方形,并在屏幕上等待一段时间。
5. 组合动画效果
```python
from manim import *
import random
class MyFirstScene(Scene):
def construct(self):
circles = []
for i in range(5):
circle = Circle().shift(2*DOWN+2*i*RIGHT)
self.add(circle)
circles.append(circle)
for i in range(10):
r = random.randint(0, 4)
self.play(
circles[r].animate.scale(1.5, about_point=circles[r].get_center()),
run_time=0.5
)
self.wait()
```
这将在屏幕上创建五个圆形,并在其中的一个圆形中心开始缩放,不断随机选择圆形进行缩放,并在屏幕上等待一段时间。