cube = [0, 1, 1, 0, 0, 1, 1, 0
0, 0, 0, 0, 1, 1, 1, 1
0, 0, 1, 1, 0, 0, 1, 1
1, 1, 1, 1, 1, 1, 1, 1];
edge = [1 2
1 5
1 4
2 3
2 6
3 4
3 7
4 8
5 6
5 8
6 7
7 8];
Note that each edge is just listed once.
Here's a function to plot our figure using MATALB's 3D routines:
function plotfig3d(pt,edge) % plots a 3D wire frame using points in pt and edges in edge M = size(edge,1); % the number of edges for i = 1:M % loop thru the edges plot3(pt(1,edge(i,1:2)), pt(2,edge(i,1:2)), pt(3,edge(i,1:2))) if (i==1), hold on, end end hold offSave this in an M-file as plotfig3d, and if you loaded the cube and edge data from above, you can plot it with:
plotfig3d(cube,edge)You should get a nice picture of a 3D cube.
To plot in 2D we need a rule (called a projection) on how to convert 3D points into 2D. The easiest version is direct projection along the y-axis. In this case a point (x,y,z) is projected to the point (x,z). A projection is a linear transformation, so we can represent it as a matrix. In homogeneous coordinates, this projection is a 3 by 4 matrix: P = [1 0 0 0; 0 0 1 0; 0 0 0 1];. There are other possibilities that we can work with later. Here's a MATLAB function for plotting with this projection:
function plotfig2d(pt,edge) % plots a 3D wire frame using points in pt and edges in edge % which have been projected into 2D M = size(edge,1); % the number of edges P = [1 0 0 0; 0 0 1 0; 0 0 0 1]; % build the projection pt = P*pt; % apply the projection for i = 1:M % loop thru the edges plot(pt(1,edge(i,1:2)), pt(2,edge(i,1:2))) if (i==1), hold on, end end hold offSave this in an M-file as plotfig2d, and if you loaded the cube and edge data from above, you can plot it with:
plotfig2d(cube,edge)For this you should just get a square as we are looking at the cube end on.
ang = pi/4; T = [1 0 0 0; 0 cos(ang) -sin(ang) 0; 0 sin(ang) cos(ang) 0; 0 0 0 1]; pt2 = T*pt; plotfig3d(pt2,edge)Just as in 2D, we can perform more complex transformations by taking the product of basic transformations.
2. Save the two plot functions and plot your object using both to test them. Make whatever changes you want to your figure to make it look good.
3. Create two transformations and call them T1 and T2. Create a 3rd transformation T3 = T1*T2. Apply each transformation to your points and create new point sets pt1, pt2 and pt3. (ptk = Tk*pt). Plot the original and 3 transformations on the same graph via:
plotfig3d(pt,edge) hold on plotfig3d(pt1,edge) hold on plotfig3d(pt2,edge) hold on plotfig3d(pt3,edge)Print this out or export for email.
4. Now plot them using 2D on 4 separate graphs using the following:
subplot(221) % uses a 2x2 grid of plots plotfig2d(pt,edge) subplot(222) % plot in 2nd spot plotfig2d(pt1,edge) subplot(223) % plot in 3rd spot plotfig2d(pt2,edge) subplot(224) % plot in 4th spot plotfig2d(pt3,edge)Print this out or export for email.