Home > Resources > Making Animation with MATLAB > 5. Creating 3D shape - Block (1)
Making Animation with MATLAB

5. Creating 3D shape - Block (1)

Let's create a block shape by combining six polygons using patch command. We first consider the case where one of the vertices coincides with the origin of the coordinate system and each side is parallel to the coordinate axis as shown in Figure 1 (we call this the "basic configuration"). In this case, if the length of each side (Lx, Ly, Lz) is given, the coordinates of the eight vertices are obvious. The six faces are defined by the following six sequences of vertices.



block_special

Figure 1: Block shape (in the basic configuration)

A MATLAB script to draw a block shape in the basic configuration is shown in Code 1. The result of the drawing by this script is shown in Figure 2.

make_block_special.m
% make_block_special.m

clear; close all;

% Side lengths
Lx = 0.15;
Ly = 0.05;
Lz = 0.30;

% Vertices
vertices = [
    0,   0,  0;  % #1
    Lx,  0,  0;  % #2
    0,  Ly,  0;  % #3
    0,   0, Lz;  % #4
    Lx, Ly,  0;  % #5
    0,  Ly, Lz;  % #6
    Lx,  0, Lz;  % #7
    Lx, Ly, Lz]; % #8

% Faces
faces = [
    1, 2, 5, 3;  % #1
    1, 3, 6, 4;  % #2
    1, 4, 7, 2;  % #3
    4, 7, 8, 6;  % #4
    2, 5, 8, 7;  % #5
    3, 6, 8, 5]; % #6

% Draw patch
figure(1);
h = patch('Faces', faces, 'Vertices', vertices, 'FaceColor', 'y');

% Axes settings
xlabel('x'); ylabel('y'); zlabel('z');
axis vis3d equal;
view([-37.5, 30]);
camlight;
grid on;
xlim([-0.15, 0.35]);
ylim([-0.2, 0.3]);
zlim([-0.1, 0.4]);
Code 1: MATLAB script to draw a block shape (in the basic configuration)


make_block_special

Figure 2: A block shape made of six polygons (in the basic configuration)
Sponsor Link