%% Section 1.6 Examples of Vector Graphics
% The triangle with vertices (0,0), (1,2), (4,3) is encoded by the 2x2 matrix

M = [0 1 4 0; 0 2 3 0] 

% I plot the triangle.

plot(M(1,:)',M(2,:)','b','LineWidth',2)
axis([0 5 0 5])
axis('square')

% I scale the horizontal direction by 2 and the vertical direction by 3 by multiplying M by the matrix D_{2,3}, then plotting the scaled triangle (red), along with the original triangle (blue).

D = [2 0; 0 3]
DM = D*M
plot(M(1,:)',M(2,:)','b','LineWidth',2)
hold on
plot(DM(1,:)',DM(2,:)','r','LineWidth',2)
hold off
axis([0 10 0 10])
axis('square')

% Next I rotate the original triangle through \frac{2\pi }{3} and \frac{4\pi }{3} by multiplying Mby R_{\frac{2\pi }{3}} and its square R_{\frac{4\pi }{3}}.

R = [cos(2*pi/3) -sin(2*pi/3)
    sin(2*pi/3) cos(2*pi/3)];
RM = R*M; R2M = R^2*M;
plot(M(1,:)',M(2,:)', 'LineWidth',2)
hold on
plot(RM(1,:)',RM(2,:)','LineWidth',2)
plot(R2M(1,:)',R2M(2,:)','LineWidth',2)
hold off
axis([-5 5 -5 5])
axis('square')

% Next I shear the original triangle, multplying Mby N_2 and its square N_4.

N = [1 2;0 1];
NM = N*M; N2M = N^2*M;
plot(M(1,:)',M(2,:)', 'LineWidth',2)
hold on
plot(NM(1,:)',NM(2,:)','LineWidth',2)
plot(N2M(1,:)',N2M(2,:)','LineWidth',2)
hold off
axis([0 17 0 4])
axis('square')

% Next I want to translate, so I have to use homogeneous coordinates. The matrix for the triangle, which I denote by \mathrm{Mt} becomes

Mt = [0 1 4 0; 0 2 3 0; 1 1 1 1]

% To translate by \left(-2,1\right)and \left(-4,2\right)I multiply Mt by T_{\left(-2,1\right)} and its square.

T = [1 0 -2; 0 1 1; 0 0 1 ]
TM = T*Mt; T2M = T^2*Mt;
plot(M(1,:)',M(2,:)', 'LineWidth',2)
hold on
plot(TM(1,:)',TM(2,:)','LineWidth',2)
plot(T2M(1,:)',T2M(2,:)','LineWidth',2)
hold off
axis([-5 4 0 6])
axis('square')

%  Finally, I apply the affine transformation obtained by first rotating by R_{\frac{\pi }{3}} the translating by \left(-2,1\right). I start by creating the rotation as a 3x3 matrix L_R.

LR = [cos(pi/3) -sin(pi/3) 0; sin(pi/3) cos(pi/3) 0;  0 0 1];
P = T*LR*Mt;
plot(M(1,:)',M(2,:)', 'LineWidth',2)
hold on
plot(P(1,:)',P(2,:)','LineWidth',2)
axis([-5 5 -1 7])
axis('square')