问题
I want to draw a simple cubic lattice using MATLAB.
I have read How to plot 3D grid (cube) in Matlab, however, I want to color every small cube.
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]
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
denotes a small white cube, whilst a 1
denotes a small black cube.
The desired result is something like this: http://www.instructables.com/id/Puzzle-Cube/

回答1:
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
Gives you this,

If you delete alpha('color');
and alphamap('rampdown');
and use axis off
, you get,

来源:https://stackoverflow.com/questions/26644918/simple-cubic-lattice-using-three-dimensional-array