How can I draw some 3d cubes in matlab

I am trying to draw 2 cubes in a single M file. This is my code:

    format compact 
    h (1) = axes ('Position', [0.2 0.2 0.6 0.6]);
    vert = [1 1 1; 1 2 1; 2 2 1; 2 1 1; ...
            1 1 2; 1 2 2; 2 2 2; 2 1 2];
    fac = [1 2 3 4; ...
        2 6 7 3; ...
        4 3 7 8; ...
        1 5 8 4; ...
        1 2 6 5; ...
        5 6 7 8];
    patch ('Faces', fac, 'Vertices', vert, 'FaceColor', 'r'); % patch function
    material shiny;
    alpha ('color');
    alphamap ('rampdown');
    view (30.30);

Now I want to draw a second cube and replace it with the first. Does anyone know how I can do this?

+5
source share
2 answers

, - : enter image description here

: 1. , . 2. , , 'hold on' 'patch'.

clf;
figure(1);
format compact 
h(1) = axes('Position',[0.2 0.2 0.6 0.6]);
vert = [1 1 -1; 
        -1 1 -1; 
        -1 1 1; 
        1 1 1; 
        -1 -1 1;
        1 -1 1; 
        1 -1 -1;
        -1 -1 -1];

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];

% I defined a new cube whose length is 1 and centers at the origin.
vert2 = vert * 0.5;  
fac2 = fac;

patch('Faces',fac,'Vertices',vert,'FaceColor','b');  % patch function
axis([-1, 1, -1, 1, -1, 1]);
axis equal;

hold on;

patch('Faces', fac2, 'Vertices', vert2, 'FaceColor', 'r');
material metal;
alpha('color');
alphamap('rampdown');
view(3);
+7

hold on...

format compact 
h(1) = axes('Position',[0.2 0.2 0.6 0.6]);
%----first cube------
vert = [1 1 1; 1 2 1; 2 2 1; 2 1 1 ; ...
        1 1 2;1 2 2; 2 2 2;2 1 2];
fac = [1 2 3 4; ...
    2 6 7 3; ...
    4 3 7 8; ...
    1 5 8 4; ...
    1 2 6 5; ...
    5 6 7 8];
patch('Faces',fac,'Vertices',vert,'FaceColor','r');  % patch function
material shiny;
alpha('color');
alphamap('rampdown');
view(30,30);

%------second cube-----
hold on;
vert2 = [1 1 1; 1 2 1; 2 2 1; 2 1 1 ; ...
            1 1 2;1 2 2; 2 2 2;2 1 2]/5;
    fac2 = [1 2 3 4; ...
        2 6 7 3; ...
        4 3 7 8; ...
        1 5 8 4; ...
        1 2 6 5; ...
        5 6 7 8];
    patch('Faces',fac2,'Vertices',vert2,'FaceColor','b');  % patch function
+2

All Articles