簡體   English   中英

如何在matlab中繪制多個3d立方體

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

我試圖在一個M文件中繪制2個立方體。 這是我的代碼:

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

現在,我想繪制第二個立方體並在第一個立方體內部替換。 有誰知道我怎么做到這一點?

也許你想要這樣的東西: 在此輸入圖像描述

你只需稍微修改你的原始代碼:1。定義一個新的立方體,它應放在第一個立方體內。 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);

使用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