Search code examples
matlabmatlab-figure

creating in matlab a fully opaque hexahedron


I want to create a fully non-transparent hexahedron (a 6 faces 3d object, like a distorted cube) using the patch function. Below is my code, (make the 3 adjacent faces of point #1 and then the other 3 adjacent to its antipodal point and thus having all 6 "patched"). However, the transparency as you can see does not work as intended. Any idea why that happens, or any way to bypass the problem?

vec=[1,2,4,3,1;1,5,6,2,1;1,5,7,3,1];
vec2=[8,4,3,7,8;8 4 2 6 8;8 6 5 7 8];
cube = [0 0 0;0 0 1;0 1 0;0 1 1; 1 0 0; 1 0 1; 1 1 0; 1 1 1];
figure
patch('Faces',[vec(1,:),vec(2,:),vec(3,:)],'Vertices',cube,'FaceColor','white','FaceAlpha',1); axis equal; cameratoolbar;
hold on
patch('Faces',[vec2(1,:),vec2(2,:),vec2(3,:)],'Vertices',cube,'FaceColor','white','FaceAlpha',1); axis equal; cameratoolbar;

This is the figure produced:enter image description here

(Matlab 2017a).


Solution

  • You created the vec array in the right way, but you screwed it when handing it to the patch() function. You can concatenate vecand vec2 in one variable. Then your code looks like this:

    vec=[1,2,4,3,1;1,5,6,2,1;1,5,7,3,1; ...
         8,4,3,7,8;8 4 2 6 8;8 6 5 7 8];
    cube = [0 0 0;0 0 1;0 1 0;0 1 1; 1 0 0; 1 0 1; 1 1 0; 1 1 1];
    patch('Faces',vec,'Vertices',cube,'FaceColor','white','FaceAlpha',1);
    axis equal
    view(-41,33);
    

    The result is shown here.
    Note: The left front side of the cube is not transparent on screen display. This is a bug of octaves figure to PNG export. Cube drawn with script