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

% =======================================
% 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: object with givenpoint (x,y) and
% scale factor
% output: scaled object

% 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 ;];
vmatrix=vertex_matrix';

givenpoint=-5*[ones(1,13);zeros(1,13)]+5*[zeros(1,13);ones(1,13)]+vmatrix;
%givenpoint is a 2 row 13 column matrix

scalefactor_x=2;
scalefactor_y=3;
sfx=scalefactor_x;
sfy=scalefactor_y;

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

% T1: translation of a vertex on object to origin
% arbitrarily choose this vertex to be the first point in givenpoint, i.e.
% the first row of the matrix givenpoint

dx=givenpoint(1,1);
dy=givenpoint(2,1);
% could also write [dx,dy]=givenpoint(1,:);

%T1: translation matrix
T1=[ 1  0  -dx;
     0  1  -dy;
    0 0 1];
 
T1point=T1*gpoint;
 
 % T2: scaling matrix 
 T2=[ sfx 0 0;
      0 sfy 0;
      0 0 1]; 
 
T2T1point=T2*T1*gpoint;  
  
%scaling done incorrectly
T2point=T2*gpoint;


 % 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(T2point(1,:),T2point(2,:),'y');
 plot(npoint(1,:),npoint(2,:),'g-');
 axis equal;
 xlabel('x');ylabel('y');
 grid on;
% title('Scale figure by factor',sfx,'in x',sfy,'in y');
 hold off;
 
