python画立方体--魔方
作者:hqx 发布时间:2022-04-22 10:20:43
标签:python,画,立方体,魔方
直接进入主题
立方体每列颜色不同:
# 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()
来源:https://blog.csdn.net/weixin_44853840/article/details/123794474
0
投稿
猜你喜欢
- Pycharm - Python 开发工具通过 agent 代理使用1、下载 Pycharm下载地址2、支持本代理包支持 2020 版本3、
- 平时在写asp代码的时候有很多重复的内容要写,麻烦的要命,比如在收集表单提交的数据时,特别是表单的输入域比较多时,要不断写好多的reques
- 在网站开发的时候经常要用chr(),但本人比较懒没时间记那么多。于是到用到的时候就查,这样麻烦。现在将它写出来方便以后用到查,也方便大家!c
- python中不存在所谓的传值调用,一切传递的都是对象的引用,也可以认为是传址。一、可变对象和不可变对象Python在heap中分配的对象分
- 基本思想:归并排序是一种典型的分治思想,把一个无序列表一分为二,对每个子序列再一分为二,继续下去,直到无法再进行划分为止。然后,就开始合并的
- np.newaxisnp.newaxis 的功能是增加新的维度,但是要注意 np.newaxis 放的位置不同,产生的矩阵形状也不同。通常按
- 内连接(inner join)。 外连接: 全连接(full join)、左连接(left join)、右连接(right join)。 交
- 1.global关键字默认情况下,在局部作用域对全局变量只能进行:读取,修改内部元素(可变类型),无法对全局变量进行重新赋值读取:CITY=
- 优雅的设计经常包含一些特殊的字体,而这些字体并不存在于用户的字体库中,我们并不能奢求每一个访客都是设计师。 :-)虽然CSS3标
- 首先说明,Supervisor 只能安装在 Python 2.x 环境中!但是基本上所有的 Linux 都同时预装了 Python 2.x
- 我打算将WebQQ单独出来运行, 一开始直接拷贝了pyxmpp2的mainloop, 但是跑起来问题多多, 所以我又研究了利用Tornado
- 没注意到MooTools的Cookie类在写的时候自己做了一次encode,在读的时候做了一次decode,在一般的情况下,这个不会有什么问
- 本文实例讲述了PHP队列用法。分享给大家供大家参考。具体分析如下:什么是队列,是先进先出的线性表,在具体应用中通常用链表或者数组来实现,队列
- 概要本文只是简单的介绍动态规划递归、非递归算法实现案例一题目一:求数组非相邻最大和[题目描述]在一个数组arr中,找出一组不相邻的数字,使得
- 前言:在很多应用场景下,我们不但需要堆的特性,例如快速知道数据最大值或最小值,同时还需要知道元素的排序信息,因此本节我们看看如何实现鱼和熊掌
- pycharm创建新文件自动添加文件头注释背景我们平时在使用pycharm发现有些大神创建一个新文件的时候会自动在文件头添加一些注释,像是有
- 本文实例为大家分享了python实现自动登录后台管理系统的具体代码,供大家参考,具体内容如下首先感谢下网络上的各位大神和博主,通过学习各位大
- 介绍文档解析涉及检查文档中的数据并提取有用的信息。它可以通过自动化减少了大量的手工工作。一种流行的解析策略是将文档转换为图像并使用计算机视觉
- #coding:utf-8 #批量修改文件名 import os import re import datetime re_st = r
- default-character-set=gbk #或gb2312,big5,utf8 然后重新启动mysql 运行->servic