问题描述
我想用 MATLAB 绘制一个简单的三次方格.
I want to draw a simple cubic lattice using MATLAB.
我已阅读如何在 Matlab 中绘制 3D 网格(立方体),但是,我想为每个小立方体着色.
I have read How to plot 3D grid (cube) in Matlab, however, I want to color every small cube.
我在MATLAB中有一个三维数组,例如,
I have a three-dimensional array in MATLAB, such as,
cube(:,:,1) = [1 0 1
0 1 1
1 1 0]
cube(:,:,2) = [0 0 1
1 1 1
0 1 0]
cube(:,:,3) = [1 1 1
0 1 1
1 0 1]
如何使用这个数组绘制简单的立方点阵,其中cube(:,:,1)
表示立方点阵的第一层,cube(:,:,2)
表示二楼,cube(:,:,3)
表示三楼.
How can I draw a simple cubic lattice using this array, in which cube(:,:,1)
denotes the first floor of the cubic lattice, cube(:,:,2)
denotes the second floor, and cube(:,:,3)
the third floor.
A 0
表示一个小的白色立方体,而一个 1
表示一个小的黑色立方体.
A 0
denotes a small white cube, whilst a 1
denotes a small black cube.
想要的结果是这样的:http://www.instructables.com/id/Puzzle-Cube/
The desired result is something like this: http://www.instructables.com/id/Puzzle-Cube/
推荐答案
我找不到更简单的东西,所以就是这样!
I couldn't find anything simpler, so this is what it is!
C = randi(2,[3 3 3])-1;
colorC = char(repmat('k',[3 3 3]));
colorC(C == 0) = 'y';
figure(1);
for x = 0 : 2
for y = 0 : 2
for z = 0 : 2
vert = [1 1 0;
0 1 0;
0 1 1;
1 1 1;
0 0 1;
1 0 1;
1 0 0;
0 0 0];
vert(:,1) = vert(:,1) + x;
vert(:,2) = vert(:,2) + y;
vert(:,3) = vert(:,3) + z;
fac = [1 2 3 4;
4 3 5 6;
6 7 8 5;
1 2 8 7;
6 7 1 4;
2 3 5 8];
patch('Faces',fac,'Vertices',vert,'FaceColor',colorC(x + 1, y + 1, z + 1));
axis([0, 3, 0, 3, 0, 3]);
alpha('color');
alphamap('rampdown');
axis equal
hold on
end
end
end
给你这个,
如果你删除 alpha('color');
和 alphamap('rampdown');
并使用 axis off
,你会得到,
If you delete alpha('color');
and alphamap('rampdown');
and use axis off
, you get,
这篇关于使用三维阵列的简单立方点阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!