简体   繁体   English

如何在matlab中绘制多个3d立方体

[英]How can I draw multiple 3d cubes in matlab

I'm trying to draw 2 cubes in one M file. 我试图在一个M文件中绘制2个立方体。 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 second cube and replace inside first one. 现在,我想绘制第二个立方体并在第一个立方体内部替换。 Does anyone know how I can do that? 有谁知道我怎么做到这一点?

Maybe you want something like this: 也许你想要这样的东西: 在此输入图像描述

you just need to slightly modify your original code: 1. define a new cube which should be placed inside the first one. 你只需稍微修改你的原始代码:1。定义一个新的立方体,它应放在第一个立方体内。 2. please remember add 'hold on' after 'patch'. 2.请记得在'补丁'之后添加'hold on'。

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

Use hold on command... 使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM