manim 在直线围成范围内填充颜色
时间: 2024-02-18 08:55:01 浏览: 22
在 manim 中,可以使用 `Polygon` 创建多边形,并使用 `fill` 方法填充颜色。以下是一个例子,演示如何在一个由直线围成的范围内填充颜色:
```python
from manim import *
class FillPolygon(Scene):
def construct(self):
# 创建一个三角形
triangle = Polygon(
ORIGIN, 3*RIGHT, 2*UP+3*RIGHT,
stroke_width = 2,
stroke_color = WHITE,
fill_opacity = 0.5,
fill_color = YELLOW
)
# 将三角形移动到屏幕中央
triangle.move_to(ORIGIN)
# 创建一个矩形
rect = Polygon(
4*LEFT, 4*LEFT+2*UP, 4*RIGHT+2*UP, 4*RIGHT,
stroke_width = 2,
stroke_color = WHITE,
fill_opacity = 0.5,
fill_color = BLUE
)
# 将矩形移动到屏幕中央
rect.move_to(ORIGIN)
# 创建一个由直线围成的范围
region = Polygon(
ORIGIN, 5*RIGHT, 5*RIGHT+2*UP, 2*UP,
stroke_width = 2,
stroke_color = WHITE,
fill_opacity = 0.5,
fill_color = RED
)
# 将范围移动到屏幕中央
region.move_to(ORIGIN)
# 将三角形、矩形和范围添加到场景中,并播放动画
self.add(triangle, rect, region)
self.wait(2)
```
在上面的代码中,我们首先创建了一个三角形、一个矩形和一个由直线围成的范围,分别使用不同的填充颜色。然后将它们添加到场景中,并使用 `wait` 方法等待一定的时间,以使动画效果更好。最终,我们可以看到三个图形都被成功地填充了颜色。
![fill_polygon](https://raw.githubusercontent.com/Elteoremadebeethoven/AnimationsWithManim/main/Gifs/fill_polygon.gif)