<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
直接進入主題
立方體每列顏色不同:
# 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畫魔方內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45