python畫立方體--魔方
直接進(jìn)入主題
立方體每列顏色不同:
# 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): ? ? """ ? ? 根據(jù)輸入生成指定尺寸的魔方 ? ? :param nx: ? ? :param ny: ? ? :param nz: ? ? :return: ? ? """ ? ? # 準(zhǔn)備一些坐標(biāo) ? ? 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 ? ? ? # 縮小間隙 ? ? # 構(gòu)建voxels頂點(diǎn)控制網(wǎng)格 ? ? # x, y, z均為6x6x8的矩陣,為voxels的網(wǎng)格,3x3x4個(gè)小方塊,共有6x6x8個(gè)頂點(diǎn)。 ? ? # 這里//2是精髓,把索引范圍從[0 1 2 3 4 5]轉(zhuǎn)換為[0 0 1 1 2 2],這樣就可以單獨(dú)設(shè)立每個(gè)方塊的頂點(diǎn)范圍 ? ? 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 ? ? ? # 給魔方六個(gè)面賦予不同的顏色 ? ? colors = np.array(['#ffd400', "#fffffb", "#f47920", "#d71345", "#145b7d", "#45b97c"]) ? ? facecolors = np.full(filled_2.shape, '#77787b') ?# 設(shè)一個(gè)灰色的基調(diào) ? ? # 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] ? ? # 前藍(lán) ? ? 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()
到此這篇關(guān)于python畫立方體--魔方的文章就介紹到這了,更多相關(guān)python畫魔方內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python+wxPython實(shí)現(xiàn)自動(dòng)生成PPTX文檔程序
這篇文章主要介紹了如何使用 wxPython 模塊和 python-pptx 模塊來編寫一個(gè)程序,用于生成包含首頁(yè)、內(nèi)容頁(yè)和感謝頁(yè)的 PPTX 文檔,感興趣的小伙伴可以學(xué)習(xí)一下2023-08-08python中使用urllib2偽造HTTP報(bào)頭的2個(gè)方法
這篇文章主要介紹了python中使用urllib2偽造HTTP報(bào)頭的2個(gè)方法,即偽造http頭信息,需要的朋友可以參考下2014-07-07python實(shí)現(xiàn)百度OCR圖片識(shí)別過程解析
這篇文章主要介紹了python實(shí)現(xiàn)百度OCR圖片識(shí)別過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01python批量獲取html內(nèi)body內(nèi)容的實(shí)例
今天小編就為大家分享一篇python批量獲取html內(nèi)body內(nèi)容的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01Python+Sympy實(shí)現(xiàn)計(jì)算微積分
微積分的計(jì)算也許平時(shí)用不到,會(huì)讓人覺得有點(diǎn)高深,它們的計(jì)算過程中需要使用很多計(jì)算規(guī)則,但是使用?Sympy?可以有效減輕這方面的負(fù)擔(dān),本文就來和大家簡(jiǎn)單講講吧2023-07-07python 匿名函數(shù)相關(guān)總結(jié)
這篇文章主要介紹了python 匿名函數(shù)的的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-03-03Python列表刪除重復(fù)元素與圖像相似度判斷及刪除實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于Python列表刪除重復(fù)元素與圖像相似度判斷及刪除的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05