% file rot2d_2.m
% ENGR 150 exercise
% reflect point around arbitrary line
% 2-D to start
% newpoint = givenpoint * composite transformation T

% =======================================
% MATLAB HELP
% to learn more about Matlab, type 'intro'
% (without quotes) at the command prompt
% also type 'demo' for more complete intro
% for help on any function, type 'helpwin functionname'
% for help on syntax:
% type 'helpwin punct' 'helpwin ops'  'helpwin arith'
% 'helpwin colon'
% for web-based help, type 'helpdesk'
% keyword search, type 'lookfor keyword'
% type variable names to get their values
% =========================================

% inputs: givenpoint (x,y) and
% axis parallel to z-axis at (dx,dy)
% output: rotated point

% enter inputs
vertex_matrix=[0 0;0 1 ;2 1 ;2 4 ;3 4 ;5 1 ;10 0 ;
  5 -1 ;3 -4 ;2 -4 ;2 -1 ;0 -1 ;0 0];
vm=vertex_matrix';

givenpoint=vm-5*[ones(1,13);zeros(1,13)]+5*[zeros(1,13);ones(1,13)];
%givenpoint is a 2 row 13 column matrix, each column is a vertex
%of the object
% the object is moved -5 in x-direction and +5 in y-direction

%axis parallel to z-axis
dx=3;
dy=2;

for rotindex=1:36

%angle of rotation
phi=rotindex*2*pi/36;


% start transformation
% add extra dimension
gpoint = [givenpoint; ones(1,13)];


% T1: translation of center(dx,dy) to origin

T1=[ 1  0  -dx;
     0  1  -dy;
     0 0   1];

 T1point=T1*gpoint;
 
 % T2: rotate about z-axis by phi
 T2=[cos(phi) -sin(phi) 0;
     sin(phi) cos(phi) 0;
    0        0        1]; 
 
 T2T1point=T2*T1point;

 % T3: translate back
 T3=[1  0  dx;
     0  1  dy;
     0  0  1];
  
 % composite transformation
 Tcomp=T3*T2*T1; 
 
 % compute new point
 npoint=Tcomp*gpoint;
 
 npoint(3,:)=[]; 
 % eliminate 3rd component of  vector 
 % using []=empty set
 % thus npoint is now 1x2
 newpoint=npoint; % 1x2 vector
            
 % plot line and points              
 hold on;          
 plot(givenpoint(1,:),givenpoint(2,:),'r-');   
 plot(T1point(1,:),T1point(2,:),'m');
 plot(T2T1point(1,:),T2T1point(2,:),'c');
 plot(npoint(1,:),npoint(2,:),'g-');
 axis equal;
 axis([-12 12 -12 12]);
 xlabel('x');ylabel('y');
 grid on;
% title('Scale figure by factor',sfx,'in x',sfy,'in y');
 hold off;
 
 F(rotindex)=getframe;
end

movie(F)
