千家信息网

python如何画立方体

发表于:2025-02-06 作者:千家信息网编辑
千家信息网最后更新 2025年02月06日,小编给大家分享一下python如何画立方体,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!立方体每列颜色不同:# Impor
千家信息网最后更新 2025年02月06日python如何画立方体

小编给大家分享一下python如何画立方体,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

立方体每列颜色不同:

# Import librariesimport matplotlib.pyplot as pltfrom mpl_toolkits.mplot3d import Axes3Dimport numpy as np    # Create axisaxes = [5,5,5]  # Create Datadata = np.ones(axes, dtype=np.bool)  # Controll Tranperencyalpha = 0.9  # Control colourcolors = np.empty(axes + [4], dtype=np.float32)  colors[0] = [1, 0, 0, alpha]  # redcolors[1] = [0, 1, 0, alpha]  # greencolors[2] = [0, 0, 1, alpha]  # bluecolors[3] = [1, 1, 0, alpha]  # yellowcolors[4] = [1, 1, 1, alpha]  # grey  # Plot figurefig = plt.figure()ax = fig.add_subplot(111, projection='3d')  # Voxels is used to customizations of# the sizes, positions and colors.ax.voxels(data, facecolors=colors, edgecolors='grey')

立方体各面颜色不同:

import matplotlib.pyplot as pltimport numpy as np  def generate_rubik_cube(nx, ny, nz):    """    根据输入生成指定尺寸的魔方    :param nx:    :param ny:    :param nz:    :return:    """    # 准备一些坐标    n_voxels = np.ones((nx + 2, ny + 2, nz + 2), dtype=bool)     # 生成间隙    size = np.array(n_voxels.shape) * 2    filled_2 = np.zeros(size - 1, dtype=n_voxels.dtype)    filled_2[::2, ::2, ::2] = n_voxels     # 缩小间隙    # 构建voxels顶点控制网格    # x, y, z均为6x6x8的矩阵,为voxels的网格,3x3x4个小方块,共有6x6x8个顶点。    # 这里//2是精髓,把索引范围从[0 1 2 3 4 5]转换为[0 0 1 1 2 2],这样就可以单独设立每个方块的顶点范围    x, y, z = np.indices(np.array(filled_2.shape) + 1).astype(float) // 2  # 3x6x6x8,其中x,y,z均为6x6x8     x[1::2, :, :] += 0.95    y[:, 1::2, :] += 0.95    z[:, :, 1::2] += 0.95     # 修改最外面的面    x[0, :, :] += 0.94    y[:, 0, :] += 0.94    z[:, :, 0] += 0.94     x[-1, :, :] -= 0.94    y[:, -1, :] -= 0.94    z[:, :, -1] -= 0.94     # 去除边角料    filled_2[0, 0, :] = 0    filled_2[0, -1, :] = 0    filled_2[-1, 0, :] = 0    filled_2[-1, -1, :] = 0     filled_2[:, 0, 0] = 0    filled_2[:, 0, -1] = 0    filled_2[:, -1, 0] = 0    filled_2[:, -1, -1] = 0     filled_2[0, :, 0] = 0    filled_2[0, :, -1] = 0    filled_2[-1, :, 0] = 0    filled_2[-1, :, -1] = 0     # 给魔方六个面赋予不同的颜色    colors = np.array(['#ffd400', "#fffffb", "#f47920", "#d71345", "#145b7d", "#45b97c"])    facecolors = np.full(filled_2.shape, '#77787b')  # 设一个灰色的基调    # facecolors = np.zeros(filled_2.shape, dtype='U7')    facecolors[:, :, -1] = colors[0]    # 上黄    facecolors[:, :, 0] = colors[1]     # 下白    facecolors[:, 0, :] = colors[2]     # 左橙    facecolors[:, -1, :] = colors[3]    # 右红    facecolors[0, :, :] = colors[4]     # 前蓝    facecolors[-1, :, :] = colors[5]    # 后绿     ax = plt.figure().add_subplot(projection='3d')    ax.voxels(x, y, z, filled_2, facecolors=facecolors)    plt.show()  if __name__ == '__main__':    generate_rubik_cube(4, 4, 4)

彩色透视立方体:

from __future__ import divisionimport numpy as npfrom mpl_toolkits.mplot3d import Axes3Dfrom mpl_toolkits.mplot3d.art3d import Poly3DCollectionfrom matplotlib.pyplot import figure, showdef quad(plane='xy', origin=None, width=1, height=1, depth=0):    u, v = (0, 0) if origin is None else origin    plane = plane.lower()    if plane == 'xy':        vertices = ((u, v, depth),                    (u + width, v, depth),                    (u + width, v + height, depth),                    (u, v + height, depth))    elif plane == 'xz':        vertices = ((u, depth, v),                    (u + width, depth, v),                    (u + width, depth, v + height),                    (u, depth, v + height))    elif plane == 'yz':        vertices = ((depth, u, v),                    (depth, u + width, v),                    (depth, u + width, v + height),                    (depth, u, v + height))    else:        raise ValueError('"{0}" is not a supported plane!'.format(plane))    return np.array(vertices)def grid(plane='xy',         origin=None,         width=1,         height=1,         depth=0,         width_segments=1,         height_segments=1):    u, v = (0, 0) if origin is None else origin    w_x, h_y = width / width_segments, height / height_segments    quads = []    for i in range(width_segments):        for j in range(height_segments):            quads.append(                quad(plane, (i * w_x + u, j * h_y + v), w_x, h_y, depth))    return np.array(quads)def cube(plane=None,         origin=None,         width=1,         height=1,         depth=1,         width_segments=1,         height_segments=1,         depth_segments=1):    plane = (('+x', '-x', '+y', '-y', '+z', '-z')             if plane is None else             [p.lower() for p in plane])    u, v, w = (0, 0, 0) if origin is None else origin    w_s, h_s, d_s = width_segments, height_segments, depth_segments    grids = []    if '-z' in plane:        grids.extend(grid('xy', (u, w), width, depth, v, w_s, d_s))    if '+z' in plane:        grids.extend(grid('xy', (u, w), width, depth, v + height, w_s, d_s))    if '-y' in plane:        grids.extend(grid('xz', (u, v), width, height, w, w_s, h_s))    if '+y' in plane:        grids.extend(grid('xz', (u, v), width, height, w + depth, w_s, h_s))    if '-x' in plane:        grids.extend(grid('yz', (w, v), depth, height, u, d_s, h_s))    if '+x' in plane:        grids.extend(grid('yz', (w, v), depth, height, u + width, d_s, h_s))    return np.array(grids)canvas = figure()axes = Axes3D(canvas)quads = cube(width_segments=4, height_segments=4, depth_segments=4)# You can replace the following line by whatever suits you. Here, we compute# each quad colour by averaging its vertices positions.RGB = np.average(quads, axis=-2)# Setting +xz and -xz plane faces to black.RGB[RGB[..., 1] == 0] = 0RGB[RGB[..., 1] == 1] = 0# Adding an alpha value to the colour array.RGBA = np.hstack((RGB, np.full((RGB.shape[0], 1), .85)))collection = Poly3DCollection(quads)collection.set_color(RGBA)axes.add_collection3d(collection)show()

以上是"python如何画立方体"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

立方体 不同 篇文章 顶点 颜色 内容 方块 网格 范围 间隙 魔方 生成 不怎么 坐标 基调 大部分 尺寸 彩色 更多 灰色 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 rust哪个是国服服务器 计算机三级网络技术证书丢了 网络安全实验报告多春红 计算机网络技术图书读后感 缅甸棋牌高防服务器 内网安全服务器 软件开发规范8566 重庆公安招聘网络安全 数据库原理设计 服务器管理器修改ftp密码 怀化学院学生网络安全守则 服务器nas是群晖吗 绍兴品诚网络技术 美国网络安全的是哪些股票 茂名网络安全资讯简单易学 中职生网络安全建设与应用 数据库完整性约束思维导图 服务器主机电源灯亮 数据库中用户定义完整性作用 深圳掘金网络技术科技有限公司 rust哪个是国服服务器 网络安全的应急演练不能跨地区 必背单词软件开发 武侯众人行网络技术服务部 东莞在线培训软件开发 乡镇学习网络安全法 手抄报网络安全合理使用手机 数据库中的二义性是什么 恶意攻击网站服务器犯法吗 陕西云淼互联网科技怎么样
0