You are on page 1of 1

CP 2Contact

How to draw a box

5
6

In CP2Contact, we need to draw the box that the particle is bouncing around
in. The question is, how can I do that in MATLAB? As a visualization task, the
only thing we can see are the edges and vertices of the box, so those become the
primary geometric descriptors of the box.
The first step is to input (or compute) the coordinates of the vertices of the box.
There are 8 vertices, which we will number 1, 2, 3, , 8 (because we will need
those numbers later). We can store the coordinates of the vertices in an array x
with each row containing the x, y, z coordinates of the 8 vertices, in order as
numbered

x1
x
x= 2

x8

y1
y2

y8

z1
z2

z8

8
7

4
3

MATLAB code
The array x has dimensions 8 by 3

%.... Establish dimensions of box


length = 2; width = 3; height = 4;

The second step is, essentially, the act of tracing the edges from vertex to vertex
without lifting the pencil from the paper. It is not possible to do that without
retracing some of the lines, but all we need to do is give a list of the vertices in
order of visitation. One possible order is

ord = [ 1, 2,3, 4,1,5, 6, 7,8,5, 6, 2,3, 7,8, 4 ]


The third step is to create an array that has as rows, the x, y, z, coordinates of
each vertex visited. So the number of rows would be the number of elements in
order. It is easy to create this array (call it box) in MATLAB

for i = 1: N
box(i, : ) = x(ord (i ), : )
end

or

box = x(ord , : )

The last step is to simply plot the box. In MATLAB the command is

plot3(box(: ,1), box(: , 2), box(: ,3))

clear; clc;

%.... Create the coordinates of the vertices


x = [0
, 0
, 0
;...
length, 0
, 0
;...
length, width, 0
;...
0
, width, 0
;...
0
, 0
, height;...
length, 0
, height;...
length, width, height;...
0
, width, height];
%.... Establish the order of visitation of the vertices
ord = [1,2,3,4,1,5,6,7,8,5,6,2,3,7,8,4];
%.... Create the box coordinate array
box = x(ord,:);
%.... Plot the box
figure(1); clf; grid on; axis equal; hold on;
p = plot3(box(:,1),box(:,2),box(:,3));
set(p,'Color','b','LineWidth',2);
az = 30; el = 15; view(az,el);

You might also like