绘制每半个不同颜色的面片立方体的有效方法

问题描述 投票:0回答:1

我需要使用 patch() 命令绘制一个立方体,其中立方体对于 y>0 是一种颜色,对于 y 是另一种颜色<0. Something like this:

我理想地希望用户能够简单地使用单个命令调用 patch(而不是循环 patch 命令),就像这样

patch(x_coords, y_coords, z_coords, colour_data)

我制作了一个工作版本,如下所示,但是任何人都可以建议一种更简洁的方法来执行此操作吗?

%%% TOP SECTION VERTICES %%%
xf1 = [1 1 1 1].';   % Face 1
xf2 = [1 1 -1 -1].'; % Face 2
xf3 = [1 1 -1 -1].'; % Face 3
xf4 = -xf1;          % Face 4
xf5 = xf2;           % Face 5

yf1 = [0 1 1 0].';
yf2 = [0 1 1 0].';
yf3 = [1 1 1 1].';
yf4 = yf1;
yf5 = yf2;

zf1 = [1 1 -1 -1].';
zf2 = [1 1 1 1].';
zf3 = [-1 1 1 -1].';
zf4 = zf1;
zf5 = -zf2;

x_half1 = [xf1 xf2 xf3 xf4 xf5];
y_half1 = [yf1 yf2 yf3 yf4 yf5];
z_half1 = [zf1 zf2 zf3 zf4 zf5];

%%% BOTTOM SECTION VERTICES
x_half2 = x_half1;
y_half2 = -y_half1;
z_half2 = z_half1;
%%%

x_verts = [x_half1 x_half2];
y_verts = [y_half1 y_half2];
z_verts = [z_half1 z_half2];

% Define patch colours
c1 = [255 0 0]/255; % Red colour
c2 = [0 0 255]/255; % Blue colour
C = zeros(10,1,3);
C(1:5,1,1) = c1(1); % First x5 faces are colour 1
C(1:5,1,2) = c1(2);
C(1:5,1,3) = c1(3);
C(6:10,1,1) = c2(1); % Next x5 faces are colour 2
C(6:10,1,2) = c2(2);
C(6:10,1,3) = c2(3);

figure;
patch(x_verts,y_verts,z_verts,C)
view(3)
arrays matlab matrix draw vertices
1个回答
0
投票

您可以定义本问题底部定义的

drawCuboid
函数,然后只需为 y 轴两侧使用不同的颜色调用它两次。该函数计算出立方体(或长方体)的 8 个顶点的位置,然后按照
patch
所需的正确顺序将它们分配给矩阵,以定义 4 个顶点的 6 个面。更多详情请参阅代码注释。

用法如下:

O     = [-1,0,-1]; % define the vertex 1 origin, note y=0
Lred  = [2,1,2];   % define the red cuboid for positive y
Lblue = [2,-1,2];  % define the blue cuboid for negative y
% Draw two cuboids
figure; hold on; grid on;
drawCuboid( O, Lblue, [0,0,1] );
drawCuboid( O, Lred,  [1,0,0] );
xlim( [-2,2] ); ylim( [-2,2] ); zlim( [-2,2] );

function drawCuboid( O, L, C )
    % Draw a cuboid with the first vertex at the origin "O" which spans 
    % the x, y and z axes by distances L(1), L(2) and L(3) respectively.
    % The distances can be negative.
    % The cube will have colour C

    % Define all verticies of the cube as the origin with or without adding
    % the length of each side. All combinations of with/without gives 8
    % vertices.
    xi = combvec([0,1],[0,1],[0,1]);
    cnr = O(:) + xi.*L(:);

    % From the order which combvec outputs the above corners, starting from
    % the origin at vertex 1, the front face clockwise is [1,5,6,2]. Then
    % the apex on the back face along the y axis is 3, and the back face
    % clockwise from the same perspective is [3,7,8,4]. With that ordering,
    % we can define all faces (one per column) in terms of the vertices:
    faces = [ 1 5 2 3 1 1
              2 6 4 4 3 2
              6 8 8 8 7 4
              5 7 6 7 5 3 ];

    % Combine the corner coordinates and apex numbers to define all faces
    X = cnr(1,:); X = X(faces);
    Y = cnr(2,:); Y = Y(faces);
    Z = cnr(3,:); Z = Z(faces);

    % Use patch to draw the cube
    patch( X, Y, Z, C )
    view(3)
end
© www.soinside.com 2019 - 2024. All rights reserved.