Computers and Technology
Adapt the procedure developed in Example 6 to rotate the square counterclockwise by incre- ments of a /9 about the origin. Stop when the square is in its original location and then rotate it in the clockwise direction until the square is in its original location again. You may want to rescale the axis by using axis ([-2,2,-2,2]). Include the M-file. Do not include the figure. Hint: Since you are performing a computation several times, you will want to use two for loops: one loop for the counterclockwise rotation and another one for the clockwise rotation. Think about how many times you will need to go through the loops, keeping in mind that you are rotating the square counterclockwise for a full circle by increments of 7/9 and then rotating the square clockwise back again. clf % clear all settings for the plot S=[0,1,1,0,0;0,0,1,1,0]; D1 = 9/8*eye (2); % dilation matrix p = plot (S(1,:), S (2,:)); % plot the square axis ([-1,4,-1,4]) % set size of the graph axis square, grid on % make the display square hold on % hold the current graph for i = 1:10 S = D1 *S; % dilate the square set(p, 'xdata', S(1,:), 'ydata',S(2,:)); % erase original figure and plot % the transformed figure pause (0.1) % adjust this pause rate to suit your computer. end D2 = 8/9*eye (2); % contraction matrix for i = 1:10 S = D2*S; % contract the square set(p, 'xdata',S(1,:), 'ydata',S(2,:)); % erase original figure and plot % the transformed figure pause (0.1) % adjust this pause rate to suit your computer. end hold off EXAMPLE 2 We can rotate the square S from Example 1 by an angle of a/4 in the counterclockwise direction by multiplying the matrix S by [cos(1/4) sin(1/4) Q = sin(/4) cos(1/4) ] The following code implements the rotation and the resulting picture is displayed in Figure 1. clear all; % clear all variables clf; % clear all settings for the plot S=[0,1,1,0,0,0,0,1,1,0]; plot (S(1,:), S (2,:), 'linewidth', 2); % plot the square hold on; theta =pi/4; % define the angle theta Q=[cos (theta),-sin (theta); sin(theta), cos (theta)]; % rotation matrix QS=Q*S; % rotate the square plot (QS (1,:), QS (2,:),'-r','linewidth', 2); % plot the rotated square title('Example of Rotation'); % add a title legend ('original square', 'rotated square') % add a legend axis equal; axis ([-1,2,-1,2]); % set the window grid on; % add a grid hold off Example of Rotation original square - rotated square -0. 5 0 0.5 1 1.5 2 Figure 1: Original square and rotated square