python动画:manim实现多面体的创建
一,介绍
内容 多面体(discusses polyhedra),主要集中在一种称为多面体的几何形状类别,并突出介绍了五种柏拉图体(Platonic solids),这些是具有特殊性质的多面体类型。
多面体
- 定义:多面体是一个三维几何体,具有平面多边形面、直边和顶点。面可以有不同的形状和大小,从而形成各种类型的多面体。
-
二,多面体的分类
1.十二面体(Dodecahedron)
- 一个有12个面的多面体,每个面都是规则的五边形。它是五个柏拉图体之一。
-
Dodecahedron(edge_length=1, **kwargs)
Dodecahedron(edge_length=1, **kwargs)
是一个表示十二面体(Dodecahedron)几何形状的函数,它通常是Python库(如matplotlib
或numpy
)中的一种创建三维几何形状的方法。以下是对这个函数及其参数的解释:函数说明
-
Dodecahedron:此函数用于创建一个十二面体对象,其面由正五边形构成。
-
参数:
edge_length
:这是一个关键字参数,用于定义十二面体的边长。在这个例子中,默认值是 1。这意味着每条边的长度为 1 单位。**kwargs
:(http://t.csdnimg.cn/HjZwV)这是一个可变关键词参数,允许用户传入其他可选参数,例如颜色、透明度、旋转角度、尺寸等。这些参数的具体取决于使用的库声明的功能。
示例1:
from manim import *class DodecahedronScene(ThreeDScene):def construct(self):self.set_camera_orientation(edge_length=1,phi=75 * DEGREES, theta=30 * DEGREES,fill_color=RED)obj = Dodecahedron()self.add(obj)
示例2:
from manim import * class DodecahedronScene01(ThreeDScene): def construct(self): # 设置初始相机角度 self.set_camera_orientation(phi=75 * DEGREES, theta=30 * DEGREES) # 创建十二面体对象 obj = Dodecahedron() # 定义每个面不同的颜色 colors = [ BLUE, GREEN, RED, YELLOW, ORANGE, PURPLE, PINK, TEAL, GOLD, MAROON, LIGHT_GRAY, DARK_GRAY ] # 给每个面上色并添加序号 for i, face in enumerate(obj.faces): face.set_fill(colors[i % len(colors)], opacity=0.75) # 设定颜色和透明度 # 为每个面添加序号 center = face.get_center() # 获取面中心 number = Tex(str(i + 1)).move_to(center).set_color(WHITE) self.add(number) # 将对象添加到场景中并旋转 self.add(obj) self.play(Rotate(obj, angle=TAU, run_time=10, rate_func=linear)) # 360度旋转 # 停顿使得我们可以看到最后的结果 self.wait()
![]() | ![]() |
![]() | ![]() |
![]() | ![]() |
2.二十面体(Icosahedron)
- 一个有20个面的多面体,每个面都是等边三角形。它也是五个柏拉图体之一。
Icosahedron(edge_length=1, **kwargs)
是 Manim 库中用于创建一个二十面体(Icosahedron)对象的构造函数。
Icosahedron(edge_length=1, **kwargs)
以下是其参数的解释:
参数解释:
- edge_length: 这是一个数值,代表二十面体每条边的长度。默认值为 1。
- kwargs: 这是可选的关键字参数,用于进一步自定义二十面体的属性,例如颜色、透明度等。例如,可以设置填充颜色、线条颜色和其他图形选项。
实现代码:
可以根据您的需求,以绿色填充表面,黑色填充顶点,红色填充边条如下:
from manim import * class IcosahedronScene011(ThreeDScene): def construct(self): # 创建一个二十面体对象 icosahedron = Icosahedron(edge_length=1) # 设置表面颜色为绿色 icosahedron.set_fill(GREEN, opacity=0.75) # 设置边条颜色为红色 icosahedron.set_stroke(RED, width=1) #icosahedron.set_color(WHITE)# 添加二十面体到场景中 self.add(icosahedron) # 设置相机与视角 self.set_camera_orientation(phi=75 * DEGREES, theta=30 * DEGREES) # 进行旋转动画 #self.play(Rotate(icosahedron, angle=TAU, run_time=10, rate_func=linear))
3.八面体(Octahedron)
- 一个有8个面的多面体,每个面都是等边三角形。它是五个柏拉图体之一。
示例1:
from manim import *class OctahedronScene(ThreeDScene):def construct(self):self.set_camera_orientation(phi=75 * DEGREES, theta=30 * DEGREES)obj = Octahedron(edge_length=2.5)self.add(obj)
4.给定坐标创建多面体
Polyhedron
是 Manim 中的一个类,用于创建多面体。它需要一些参数来定义其形状和外观。以下是参数的详细解释:
参数说明
-
vertex_coords
:- 类型:
list of tuples
- 描述:一个列表,包含多面体的每个顶点的坐标。例如,
[(1, 1, 1), (1, -1, 1), ...]
。
- 类型:
-
faces_list
:- 类型:
list of lists
- 描述:一个列表,定义多面体的面。每个面由顶点的索引组成,表示该面是由哪些顶点构成。例如,
[[0, 1, 2], [0, 2, 3], ...]
。
- 类型:
-
faces_config
:- 类型:
dict
(可选) - 描述:用于配置面外观的字典,例如颜色、透明度等。默认为空字典。
- 类型:
-
graph_config
:- 类型:
dict
(可选) - 描述:用于配置边和顶点的外观的字典,例如颜色、线宽等。默认为空字典。
- 类型:
示例1:
from manim import * class SquarePyramidScene(ThreeDScene): def construct(self): # 设置摄像机视角 self.set_camera_orientation(phi=75 * DEGREES, theta=30 * DEGREES) # 定义顶点坐标 vertex_coords = [ [1, 1, 0], # 底面顶点1 [1, -1, 0], # 底面顶点2 [-1, -1, 0], # 底面顶点3 [-1, 1, 0], # 底面顶点4 [0, 0, 3], # 顶点 [0, 0, -3] # 底部顶点 ] # 定义面,使用顶点索引 faces_list = [ [0, 1, 4], # 面1 [1, 2, 4], # 面2 [2, 3, 4], # 面3 [3, 0, 4], # 面4 [0, 1, 2, 3, 5] # 底面 ] # 创建多面体 pyramid = Polyhedron(vertex_coords, faces_list) # 添加多面体到场景 self.add(pyramid)
示例2:
下面的示例代码展示了如何使用 Polyhedron
创建一个简单的三角形面体,同时对面、边和顶点进行着色:
from manim import * class SquarePyramidScene01 (ThreeDScene): def construct(self): self.set_camera_orientation(phi=65 * DEGREES, theta=30 * DEGREES) # 定义顶点坐标 vertex_coords = [ (1, 1, 1), (1, -1, 1), (-1, -1, 1), (-1, 1, 1), (0, 0, -3), ] # 定义面,使用顶点索引 faces_list = [ [0, 1, 4], [1, 2, 4], [2, 3, 4], [3, 0, 4], [0, 1, 2, 3], # 顶面 ] # 创建多面体 polyhedron = Polyhedron(vertex_coords, faces_list) polyhedron.set_fill(RED, opacity=0.5) polyhedron.set_stroke(YELLOW, width=1)# 添加多面体到场景 self.add(polyhedron)
5.四面体(正四面体)
Tetrahedron
是一个用于创建四面体(正四面体)对象的类。它可以在 3D 动画库 Manim 中使用,以便可视化和动画展示。以下是有关 Tetrahedron
类及其参数的详细解释:
函数及参数解释
Tetrahedron(edge_length=1, **kwargs)
1. edge_length
- 类型:浮点数(
float
) - 默认值:1
- 说明:这是四面体每条边的长度。如果你希望生成一个不同大小的四面体,只需设置该参数为所需的边长。例如,
edge_length=2
将创建一个边长为 2 的四面体。
2. **kwargs
- 类型:关键字参数(
dict
) - 说明:这些是可选的额外参数,用于自定义四面体对象的属性。
kwargs
可以包括各种属性,具体取决于 Manim 版本和Tetrahedron
类的实现。有些常见的参数包括:fill_color
: 用于设置面填充的颜色。stroke_color
: 用于设置边的颜色。opacity
: 用于设置面颜色的透明度。stroke_width
: 用于设置边的宽度。
例子:
这是一个示例,展示如何使用 Tetrahedron
类创建一个四面体并设置一些关键字参数:
from manim import * class TetrahedronScene01(ThreeDScene): def construct(self):self.set_camera_orientation(phi=-125 * DEGREES, theta=30 * DEGREES) # 创建一个边长为 2 的四面体,面为绿色,边为白色 tetrahedron = Tetrahedron(edge_length=2)tetrahedron.set_fill=GREENtetrahedron.set_stroke(WHITE, opacity=1)tetrahedron.set_stroke_width=2 # 添加到场景 self.add(tetrahedron)