前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python实现动态3D立方体:旋转的3D立方体动画

Python实现动态3D立方体:旋转的3D立方体动画

作者头像
屿小夏
发布2024-09-25 08:40:36
790
发布2024-09-25 08:40:36
举报
文章被收录于专栏:IT杂谈学习

引言

3D动画在数据可视化和图形学中具有重要意义,能够生动地展示复杂的三维结构和运动。在这篇博客中,我们将使用Python来实现一个动态旋转的3D立方体。通过利用Matplotlib库,我们能够轻松创建和动画化3D立方体。

准备工作

前置条件

在开始之前,你需要确保你的系统已经安装了Matplotlib库。如果你还没有安装它,可以使用以下命令进行安装:

代码语言:javascript
复制
pip install matplotlib

Matplotlib是一个强大的Python绘图库,支持生成各种静态、动态和交互式的图形。

代码实现与解析

导入必要的库

我们首先需要导入Matplotlib库和其他必要的模块:

代码语言:javascript
复制
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.animation import FuncAnimation
初始化3D立方体

我们需要定义3D立方体的顶点和边:

代码语言:javascript
复制
# 定义立方体的顶点
vertices = np.array([
    [1, 1, 1],
    [1, 1, -1],
    [1, -1, 1],
    [1, -1, -1],
    [-1, 1, 1],
    [-1, 1, -1],
    [-1, -1, 1],
    [-1, -1, -1]
])

# 定义立方体的边
edges = [
    [0, 1], [0, 2], [0, 4], [1, 3], [1, 5],
    [2, 3], [2, 6], [3, 7], [4, 5], [4, 6],
    [5, 7], [6, 7]
]
绘制立方体

我们定义一个函数来绘制立方体:

代码语言:javascript
复制
def draw_cube(ax, vertices, edges):
    for edge in edges:
        points = vertices[edge]
        ax.plot3D(*zip(*points), color="b")
旋转立方体

我们定义一个旋转矩阵来旋转立方体:

代码语言:javascript
复制
def rotate(vertices, angle_x, angle_y, angle_z):
    # 旋转矩阵
    rotation_x = np.array([
        [1, 0, 0],
        [0, np.cos(angle_x), -np.sin(angle_x)],
        [0, np.sin(angle_x), np.cos(angle_x)]
    ])
    rotation_y = np.array([
        [np.cos(angle_y), 0, np.sin(angle_y)],
        [0, 1, 0],
        [-np.sin(angle_y), 0, np.cos(angle_y)]
    ])
    rotation_z = np.array([
        [np.cos(angle_z), -np.sin(angle_z), 0],
        [np.sin(angle_z), np.cos(angle_z), 0],
        [0, 0, 1]
    ])
    # 应用旋转
    return np.dot(vertices, rotation_x).dot(rotation_y).dot(rotation_z)
创建动画

我们使用FuncAnimation创建动画效果:

代码语言:javascript
复制
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

def update(frame):
    ax.cla()
    angle = np.radians(frame)
    rotated_vertices = rotate(vertices, angle, angle, angle)
    draw_cube(ax, rotated_vertices, edges)
    ax.set_xlim([-2, 2])
    ax.set_ylim([-2, 2])
    ax.set_zlim([-2, 2])

ani = FuncAnimation(fig, update, frames=360, interval=30)
plt.show()

完整代码

代码语言:javascript
复制
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.animation import FuncAnimation

# 定义立方体的顶点
vertices = np.array([
    [1, 1, 1],
    [1, 1, -1],
    [1, -1, 1],
    [1, -1, -1],
    [-1, 1, 1],
    [-1, 1, -1],
    [-1, -1, 1],
    [-1, -1, -1]
])

# 定义立方体的边
edges = [
    [0, 1], [0, 2], [0, 4], [1, 3], [1, 5],
    [2, 3], [2, 6], [3, 7], [4, 5], [4, 6],
    [5, 7], [6, 7]
]

# 绘制立方体函数
def draw_cube(ax, vertices, edges):
    for edge in edges:
        points = vertices[edge]
        ax.plot3D(*zip(*points), color="b")

# 旋转立方体函数
def rotate(vertices, angle_x, angle_y, angle_z):
    # 旋转矩阵
    rotation_x = np.array([
        [1, 0, 0],
        [0, np.cos(angle_x), -np.sin(angle_x)],
        [0, np.sin(angle_x), np.cos(angle_x)]
    ])
    rotation_y = np.array([
        [np.cos(angle_y), 0, np.sin(angle_y)],
        [0, 1, 0],
        [-np.sin(angle_y), 0, np.cos(angle_y)]
    ])
    rotation_z = np.array([
        [np.cos(angle_z), -np.sin(angle_z), 0],
        [np.sin(angle_z), np.cos(angle_z), 0],
        [0, 0, 1]
    ])
    # 应用旋转
    return np.dot(vertices, rotation_x).dot(rotation_y).dot(rotation_z)

# 创建动画
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

def update(frame):
    ax.cla()
    angle = np.radians(frame)
    rotated_vertices = rotate(vertices, angle, angle, angle)
    draw_cube(ax, rotated_vertices, edges)
    ax.set_xlim([-2, 2])
    ax.set_ylim([-2, 2])
    ax.set_zlim([-2, 2])

ani = FuncAnimation(fig, update, frames=360, interval=30)
plt.show()
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-09-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 引言
  • 准备工作
    • 前置条件
    • 代码实现与解析
      • 导入必要的库
        • 初始化3D立方体
          • 绘制立方体
            • 旋转立方体
              • 创建动画
              • 完整代码
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档