Home > Resources > Making Animation with MATLAB > 4. Creating polygons with patch
Making Animation with MATLAB

4. Creating polygons with patch

To make an animation of a rigid body, we first have to create a shape that represents the rigid body. In MATLAB we can use patch command to create an arbitrary three-dimensional shape. The patch command creates one or more polygonal surfaces (patch object) defined by three or more vertices. By combining patch objects we can create an arbitrary polyhedron.

For example, Code 1 is a MATLAB script that draws a triangle patch with three vertices at (1,0,0), (0,2,0), and (0,0,3). The result of the drawing by this script is shown in Figure 1.

test_patch.m
% test_patch.m

clear; close all;

% Define vertices
vertices = [
    1, 0, 0;  % vertice #1
    0, 2, 0;  % vertice #2
    0, 0, 3]; % vertice #3

% Define faces
faces = [
    1, 2, 3]; % face #1

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

% Axes settings
xlabel('x'); ylabel('y'); zlabel('z');
axis vis3d equal;
view([142.5, 30]);
camlight;
grid on;
Code 1: Example MATLAB script that draws a triangle patch with vertices at (1,0,0), (0,2,0), (0,0,3)


test_patch

Figure 1: A triangle patch with vertices at (1,0,0), (0,2,0), (0,0,3)
Sponsor Link