python画立方体--魔方
更新时间:2022年3月29日 14:03 点击:317
直接进入主题
立方体每列颜色不同:
# Import libraries import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import numpy as np # Create axis axes = [5,5,5] # Create Data data = np.ones(axes, dtype=np.bool) # Controll Tranperency alpha = 0.9 # Control colour colors = np.empty(axes + [4], dtype=np.float32) colors[0] = [1, 0, 0, alpha] # red colors[1] = [0, 1, 0, alpha] # green colors[2] = [0, 0, 1, alpha] # blue colors[3] = [1, 1, 0, alpha] # yellow colors[4] = [1, 1, 1, alpha] # grey # Plot figure fig = 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 plt import 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 division import numpy as np from mpl_toolkits.mplot3d import Axes3D from mpl_toolkits.mplot3d.art3d import Poly3DCollection from matplotlib.pyplot import figure, show def 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] = 0 RGB[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画立方体--魔方的文章就介绍到这了,更多相关python画魔方内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!
原文出处:https://blog.csdn.net/weixin_44853840/article/details/123794
相关文章
- 这篇文章主要介绍了python-opencv-画外接矩形框的实例代码,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-09-04
- ps动态环绕动画效果是现在很多人都非常喜欢的,大多数人还不知道ps动态环绕动画效果怎么制作下面文章就给大家介绍下ps怎么制作科技感十足的动态环绕动画效果,一起来看看...2017-07-06
Python astype(np.float)函数使用方法解析
这篇文章主要介绍了Python astype(np.float)函数使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-08- 2022虎年新年即将来临,小编为大家带来了一个利用Python编写的虎年烟花特效,堪称全网最绚烂,文中的示例代码简洁易懂,感兴趣的同学可以动手试一试...2022-02-14
- 在本篇文章里小编给大家分享的是一篇关于python中numpy.empty()函数实例讲解内容,对此有兴趣的朋友们可以学习下。...2021-02-06
python-for x in range的用法(注意要点、细节)
这篇文章主要介绍了python-for x in range的用法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-05-10- 这篇文章主要介绍了Python 图片转数组,二进制互转操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-09
- 这篇文章主要介绍了Python中的imread()函数用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-16
- 这篇文章主要介绍了python如何实现b站直播自动发送弹幕,帮助大家更好的理解和学习使用python,感兴趣的朋友可以了解下...2021-02-20
python Matplotlib基础--如何添加文本和标注
这篇文章主要介绍了python Matplotlib基础--如何添加文本和标注,帮助大家更好的利用Matplotlib绘制图表,感兴趣的朋友可以了解下...2021-01-26- 这篇文章主要介绍了解决python 使用openpyxl读写大文件的坑,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-13
- 今天小编就为大家分享一篇python 计算方位角实例(根据两点的坐标计算),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-04-27
- 这篇文章主要为大家详细介绍了python实现双色球随机选号,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-05-02
- 在本篇文章里小编给大家整理的是一篇关于python中使用np.delete()的实例方法,对此有兴趣的朋友们可以学习参考下。...2021-02-01
- 这篇文章主要介绍了使用Python的pencolor函数实现渐变色功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-09
Python getsizeof()和getsize()区分详解
这篇文章主要介绍了Python getsizeof()和getsize()区分详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-20- 这篇文章主要介绍了python自动化办公操作PPT的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-05
- 这篇文章主要介绍了解决python 两个时间戳相减出现结果错误的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-12
- 这篇文章主要为大家详细介绍了python实现学生通讯录管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-25
- 这篇文章主要介绍了PyTorch一小时掌握之迁移学习篇,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-09-08