1

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/
enter image description here

Community
  • 1
  • 1
Kongling
  • 65
  • 6

1 Answers1

4

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,

enter image description here

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

enter image description here

Rashid
  • 4,326
  • 2
  • 29
  • 54
  • I can not reappear the figure above by the code you get. However, I find the best answer, please see http://emuch.net/bbs/viewthread.php?tid=8090152&viewexp=yes – Kongling Oct 31 '14 at 14:41
  • @Kongling, Come on! just copy and paste the code. `C` in my code is your `cube`. – Rashid Oct 31 '14 at 19:15
  • Sorry, I have tried accordding to your said. However, I only got the axes. – Kongling Nov 01 '14 at 07:06
  • Thank you for your patient reply, I have generated the picture by copying and pasting the code you wrote. – Kongling Nov 01 '14 at 13:50
  • @Kongling, Thanks God. I'm glad it finally worked. Is it what you wanted? – Rashid Nov 01 '14 at 13:55
  • Thank you, I have another question, could I add the number on the surface of the small cubic in the figure, if I have interger number such as 0,1,2,3 in the matrix, different color is applied to different number. – Kongling Nov 02 '14 at 03:54
  • @Kongling, please explain more, I couldn't understand. You mean you want something other than cube? or other colors? – Rashid Nov 02 '14 at 04:01
  • I have a three-dimentional array contain the interger number such as 0,1,2,3 and so on, and I want to draw a simple cubic lattice, each lattice corresponds to each number. Each color corresponds to each number, and the number is labeled on the external surface of every small lattice. – Kongling Nov 02 '14 at 06:16
  • like this, http://xiangce.baidu.com/picture/album/list/5da0e272c7b725cba5ae550a979cb03a8068fec3#picSign=1c05253c9f07b4bd1378d117c418894c77f6e7f6 – Kongling Nov 02 '14 at 06:19
  • @Kongling, That would be a little harder. maybe accept this answer and ask another question. – Rashid Nov 02 '14 at 06:20
  • @Kongling, I couldn't open the web page, It counts down and changes the website. maybe you could email me the picture. – Rashid Nov 02 '14 at 06:23