%this script demonstrates linear transformations
%PC MA 302
%%
%SCALING
clc;
clf;
clear
S = [-1 1 1+sqrt(2) 1+sqrt(2) 1.0 -1.0 -1*(1+sqrt(2)) -1.0*(1+sqrt(2))
     -1*(1+sqrt(2)) -1*(1+sqrt(2)) -1 1 1+sqrt(2) 1 + sqrt(2) 1 -1];
hold on
connect_dots(S,10);
connect_dots(4*S,10);
connect_dots(0.25*S,10);
legend('Original','Dilation (4)','Contraction ($\frac{1}{4}$)','Interpreter','latex');
xlabel('x','Interpreter','latex');
ylabel('y','Interpreter','latex');
title('Scaling');
hold off
%%
%shear in x direction
clc
clf
clear

S = [-1 1 1+sqrt(2) 1+sqrt(2) 1.0 -1.0 -1*(1+sqrt(2)) -1.0*(1+sqrt(2))
     -1*(1+sqrt(2)) -1*(1+sqrt(2)) -1 1 1+sqrt(2) 1 + sqrt(2) 1 -1];
T=[1 2.5; 0 1];
connect_dots(T*S,10);
hold on
connect_dots(S,10);
plot(1.0,1+sqrt(2),'kd','MarkerSize',10);
plot(1+sqrt(2),1,'k*','MarkerSize',10);

p1 = T*[1.0;1+sqrt(2)];
p2 = T*[1+sqrt(2);1];
plot(p1(1),p1(2),'kd','MarkerSize',10);
plot(p2(1),p2(2),'k*','MarkerSize',10);
title('Shear');
hold off
%%
%combinations
%rotation, reflection and scaling
theta = pi/8;
RF=[1 0; 0 -1];
RT= [cos(theta) -sin(theta);
    sin(theta) cos(theta)];
connect_dots((2*(RF*(RT*S))),10); %%notice the order here.
hold on
connect_dots(S,10);
%plot 2 original coords
plot(1.0,1+sqrt(2),'kd','MarkerSize',10);
plot(1+sqrt(2),1,'k*','MarkerSize',10);
%transformed points
p1 = 2*RF*RT*[1.0;1+sqrt(2)];
p2 = 2*RF*RT*[1+sqrt(2);1];
plot(p1(1),p1(2),'kd','MarkerSize',10);
plot(p2(1),p2(2),'k*','MarkerSize',10);
yline(0,'--');
hold off

