% Use % % help plot3 % % to get some documentation on plotting. % % The command % % plot3(x,y,z) % % draws a polygon through the points % (x(1),y(1),z(1)) ,..., (x(n),y(n),z(n)), % where x,y,z are column vectors of length n. % % To get a cube, imagine tracing the edges in sequence % and then put the vertex coordinates into x,y, and z % in the order they are encountered. It is OK to retrace edges. % % The following solution with n=16 retraces 3 edges: % Vertices (as row vectors) in the order they are encountered: u0 = [ 0 0 0; 1 0 0; 1 0 1; 1 1 1; 1 1 0; 0 1 0; 0 1 1; 0 0 1; 0 0 0; 0 1 0; 0 1 1; 1 1 1; 1 1 0; 1 0 0; 1 0 1; 0 0 1; 0 0 0] % NOTE: Since the vertices are row vectors in this formulation, % we must transpose u before applying a rotation matrix % on the left, then transpose back before plotting the vertices. % Clear the current figure clf % Copy columns into x,y,z and plot the original cube with black lines: x=u0(:,1); y=u0(:,2); z=u0(:,3); plot3(x,y,z,'Color', 'black') % Set the axes big enough to see the cube in context: lims = [-2 2]; axis([lims lims lims]); axis manual; % Hold multiple plots in the same figure hold on % Identify the X, Y, and Z axes with labels: xlabel("X"); ylabel("Y"); zlabel("Z"); % Make the rotation matrices alpha = pi/3; c=cos(alpha); s=sin(alpha); Rx=[1 0 0; 0 c -s; 0 s c]; beta = -pi/4; c=cos(beta); s=sin(beta); Ry=[c 0 s; 0 1 0; -s 0 c]; gamma = 0.5; c=cos(gamma); s=sin(gamma); Rz=[c -s 0; s c 0; 0 0 1]; % Apply the rotation matrices to the original cube's vertices and plot % the results using three different colors: % NOTE: here we use the lemma: (R*u')' = u*R' u = u0*Rx'; x=u(:,1); y=u(:,2); z=u(:,3); plot3(x,y,z,'Color', 'red') u = u0*Ry'; x=u(:,1); y=u(:,2); z=u(:,3); plot3(x,y,z,'Color', 'green') u = u0*Rz'; x=u(:,1); y=u(:,2); z=u(:,3); plot3(x,y,z,'Color', 'blue')