Simple cubic lattice using a three-dimensional array

I want to draw a simple cubic lattice using MATLAB.

I read How to build a three-dimensional grid (cube) in Matlab , however I want to color each small cube.

I have a three dimensional array in MATLAB, for example

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) third floor.

A 0 denotes a small white cube, and a 1 denotes a small black cube.

The desired result looks something like this: http://www.instructables.com/id/Puzzle-Cube/
enter image description here

+1
source share
1 answer

I could not find anything easier, so here 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 remove alpha('color'); and alphamap('rampdown'); and use axis off , you get

enter image description here

+4
source

Source: https://habr.com/ru/post/1215992/


All Articles