Upload
ar-bangash
View
214
Download
1
Embed Size (px)
DESCRIPTION
CP 2 How to Draw a Box
Citation preview
In CP2—Contact, 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
CP 2—ContactHow to draw a box
lw
h
xy
z
4
2
6 7
85
1
3
[ ]1,2,3,4,1,5,6,7,8,5,6,2,3,7,8,4=ord
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
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
1 1 1
2 2 2
8 8 8
x y zx y z
x y z
=
x
for 1:( , : ) ( ( ), : )
end
i Ni i
==box x ord
The last step is to simply plot the box. In MATLAB the command is
plot3( (: ,1), (: , 2), (: ,3))box box box
( , : )or =box x ord
clear; clc;
%.... Establish dimensions of boxlength = 2; width = 3; height = 4;
%.... 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);
MATLAB code
The array x has dimensions 8 by 3