%Rotating Rectangle for CMSC104Y 10/19/2012
% This script needs no inputs, but will clear all existing figures
% EFCL 10/19/2012
%

close all % close any active plots
line=['r','g','k','b']; % set up line colors
Rectangle = [ -1     1   1    -1   -1;
              -0.5 -0.5  0.5  0.5  -0.5]  % make the basic rectangle

plot(Rectangle(1,:),Rectangle(2,:),'bs-','LineWidth',2); %plot the basic rectangle
axis([-2 2 -2 2]); % set the plotting axes
axis('square'); % make sure they are square!
axis off % and turn them off so we only see the plot
title('Basic Rectangle'); % title the intial plot
figure(1) % show the figure
disp(['Waiting...']) %display a message in the command window
pause(3); % wait for 3 seconds
close(1); %close the first figure

for k=0:15  % do 16 different positions
    phi=k*pi/16; % establish the angle of rotation
    A=[cos(phi) -sin(phi);sin(phi) cos(phi)]; %General form of a 2-D rotation
    RotatedRectangle = A*Rectangle % rotate with a matrix multiplication
    plot(RotatedRectangle(1,:),RotatedRectangle(2,:),line(mod(k,4)+1)); 
    
    % do some plot scaling, etc., but only on the first time through the
    % loop
    if k==0
        axis([-2 2 -2 2]);
        axis('square');
        axis off
        hold on % keep the current plot
    end;
    
    figure(1); %display the plot
    disp(['Waiting, phi = ',num2str(phi)]); %message to command window
    %pause(1); % wait for 1 second
end; % end of loop, do the next k 
hold off; % release the current plot
figure(1) % display the current plot
disp(['Waiting to start second loop...']); %message to command window
pause % wait until a key is pushed
for k=0:64  % for 16 different rotations and offsets
    phi=k*pi/16; % angle of rotation in radians
    A=[cos(phi) -sin(phi);sin(phi) cos(phi)]; %rotation matrix
    RotatedRectangle=A*Rectangle + k/16*ones(size(Rectangle));  % rotate and offset
    plot(RotatedRectangle(1,:),RotatedRectangle(2,:),line(mod(k,4)+1)); 
    
    % do the plot maintenance actions again
    if k==0
        axis([-8 8 -8 8]);
        axis('square');
        axis off
        hold on % keep the current plot
    end;
    figure(1);
    disp(['Waiting phi = ',num2str(phi),' offset = ',num2str(k/16)]);
    %pause(1); 
end;
    

