%% Lab 3

V = [-1 -1;
      1 -1;
      1  1;
     -1  1];
F = [1 2 3;
     1 3 4];
C = [1 0 0;
     1 0 0];

%% Question 1 Translations in 2D commute
% Translating an object by a translation d1 followed by a translation
% d2 yields the same results as translating by d2 followed by d1.

% d1 followed by d2
d1 = [3 1];
d2 = [-4 -4];
W = V + repmat(d1, [size(V, 1) 1]);
X = W + repmat(d2, [size(V, 1) 1]);
C1 = [0 0 1;
      0 0 1];
C2 = [0 1 0;
      0 1 0];
  
patch('Faces', F, 'Vertices', V, 'FaceVertexCData', C, 'FaceColor', 'flat');
patch('Faces', F, 'Vertices', W, 'FaceVertexCData', C1, 'FaceColor', 'flat');
patch('Faces', F, 'Vertices', X, 'FaceVertexCData', C2, 'FaceColor', 'flat');
axis([-5 5 -5 5]);
axis square
line([0 d1(1)], [0 d1(2)], 'Color', 'k', 'LineWidth', 2);
line([d1(1) (d1(1) + d2(1))], [d1(2) (d1(2) + d2(2))], 'Color', 'k', 'LineWidth', 2);
legend('start', 'intermediate', 'final')
title('d1 followed by d2')


% d2 followed by d1
figure
W = V + repmat(d2, [size(V, 1) 1]);
X = V2 + repmat(d1, [size(V, 1) 1]);
C1 = [0 0 1;
      0 0 1];
C2 = [0 1 0;
      0 1 0];
  
patch('Faces', F, 'Vertices', V, 'FaceVertexCData', C, 'FaceColor', 'flat');
patch('Faces', F, 'Vertices', W, 'FaceVertexCData', C1, 'FaceColor', 'flat');
patch('Faces', F, 'Vertices', X, 'FaceVertexCData', C2, 'FaceColor', 'flat');
axis([-5 5 -5 5]);
axis square
line([0 d2(1)], [0 d2(2)], 'Color', 'k', 'LineWidth', 2);
line([d2(1) (d1(1) + d2(1))], [d2(2) (d1(2) + d2(2))], 'Color', 'k', 'LineWidth', 2);
legend('start', 'intermediate', 'final')
title('d2 followed by d1')


%% Question 2 Rotations in 2D commute
% Rotating an object by an angle theta1 followed by an angle theta2
% yields the same results as rotating by theta2 followed by rotating
% by theta1.

% R1 followed by R2
ang1 = 10;
ang2 = -60;
R1 = [cosd(ang1) -sind(ang1);
      sind(ang1)  cosd(ang1)];
R2 = [cosd(ang2) -sind(ang2);
      sind(ang2)  cosd(ang2)];

W = (R1 * V')';
X = (R2 * W')';
C1 = [0 0 1;
      0 0 1];
C2 = [0 1 0;
      0 1 0];
  
figure(1); clf
patch('Faces', F, 'Vertices', V, 'FaceVertexCData', C, 'FaceColor', 'flat');
patch('Faces', F, 'Vertices', W, 'FaceVertexCData', C1, 'FaceColor', 'flat');
patch('Faces', F, 'Vertices', X, 'FaceVertexCData', C2, 'FaceColor', 'flat');
axis([-5 5 -5 5]);
axis square
legend('start', 'intermediate', 'final')
title('R1 followed by R2')


% R2 followed by R1
figure(2); clf
W = (R2 * V')';
X = (R1 * W')';
C1 = [0 0 1;
      0 0 1];
C2 = [0 1 0;
      0 1 0];
  
patch('Faces', F, 'Vertices', V, 'FaceVertexCData', C, 'FaceColor', 'flat');
patch('Faces', F, 'Vertices', W, 'FaceVertexCData', C1, 'FaceColor', 'flat');
patch('Faces', F, 'Vertices', X, 'FaceVertexCData', C2, 'FaceColor', 'flat');
axis([-5 5 -5 5]);
axis square
legend('start', 'intermediate', 'final')
title('R2 followed by R1')

%% Question 3 Transformations in general do not commute
% In general, transformations do not commute. For example, translating
% followed by rotation yields a different result than rotation followed
% by translation.

% rotation matrix
ang1 = 80;
R1 = [cosd(ang1) -sind(ang1);
      sind(ang1)  cosd(ang1)];

% translate then rotate
W = V + repmat(d1, [size(V, 1) 1]);
X = (R1 * W')';
figure(1); clf
patch('Faces', F, 'Vertices', V, 'FaceVertexCData', C, 'FaceColor', 'flat');
patch('Faces', F, 'Vertices', W, 'FaceVertexCData', C1, 'FaceColor', 'flat');
patch('Faces', F, 'Vertices', X, 'FaceVertexCData', C2, 'FaceColor', 'flat');
axis([-5 5 -5 5]);
axis square
legend('start', 'intermediate', 'final')
title('d1 followed by R1')

% rotate then translate
W = (R1 * V')';
X = W + repmat(d1, [size(V, 1) 1]);
figure(2); clf
patch('Faces', F, 'Vertices', V, 'FaceVertexCData', C, 'FaceColor', 'flat');
patch('Faces', F, 'Vertices', W, 'FaceVertexCData', C1, 'FaceColor', 'flat');
patch('Faces', F, 'Vertices', X, 'FaceVertexCData', C2, 'FaceColor', 'flat');
axis([-5 5 -5 5]);
axis square
legend('start', 'intermediate', 'final')
title('R1 followed by d1')


%% Question 4 2D translation can be written as matrix multiplication
% Translation can be written as matrix multiplication using homogeneous
% coordinates.

% translation as vector addition
W = V + repmat(d1, [size(V, 1) 1]);
figure(1); clf
patch('Faces', F, 'Vertices', V, 'FaceVertexCData', C, 'FaceColor', 'flat');
patch('Faces', F, 'Vertices', W, 'FaceVertexCData', C1, 'FaceColor', 'flat');
axis([-5 5 -5 5]);
axis square
legend('start', 'final')
title('d1 using vector addition')

% translation as matrix multiplication
D = [eye(2) d1';
     0 0 1];
Vh = [V'; ones(1, size(V, 1))];
Qh = D * Vh;
X = Qh(1:2, :)';
figure(2); clf
patch('Faces', F, 'Vertices', V, 'FaceVertexCData', C, 'FaceColor', 'flat');
patch('Faces', F, 'Vertices', X, 'FaceVertexCData', C2, 'FaceColor', 'flat');
axis([-5 5 -5 5]);
axis square
legend('start', 'final')
title('d1 using matrix multiplication')


%% Question 5 A sequence of transformations can be written as matrix
%% multiplication

% scale
sx = 2;
sy = 0.25;
S = [sx 0;
     0  sy];
X = (S * V')';
figure(1); clf
patch('Faces', F, 'Vertices', V, 'FaceVertexCData', C, 'FaceColor', 'flat');
Cscale = C1;
patch('Faces', F, 'Vertices', X, 'FaceVertexCData', Cscale, 'FaceColor', 'flat');

% rotation
R = R1;
X = (R * X')' ;
Crot = [1 1 0; 1 1 0];
patch('Faces', F, 'Vertices', X, 'FaceVertexCData', Crot, 'FaceColor', 'flat');

% translation
X = X + repmat(d1, [size(V, 1) 1]);
Cfinal = C2;
patch('Faces', F, 'Vertices', X, 'FaceVertexCData', Cfinal, 'FaceColor', 'flat');
axis([-5 5 -5 5]);
axis square
legend('start', 'scale', 'rotation', 'translation')
title('scale followed by rotation followed by translation')


% sequence as homogenous matrix multiplication
R = [R [0 0]'; 0 0 1];
S = [S [0 0]'; 0 0 1];
Qh = D * R * S * Vh;
X = Qh(1:2, :)';
figure(2); clf
patch('Faces', F, 'Vertices', V, 'FaceVertexCData', C, 'FaceColor', 'flat');
patch('Faces', F, 'Vertices', X, 'FaceVertexCData', Cfinal, 'FaceColor', 'flat');
axis([-5 5 -5 5]);
axis square
legend('start', 'final')
title('matrix multiplication')



%% Question 6 Rotations in 3D do not commute
% Unlike 2D, rotations in 3D do not commute. For example, rotation
% about the z axis followed by rotation about the y axis yields a
% different result than rotation about the y axis followed by
% rotation about the z axis.

% cube
V = [-1 -1  1;
      1 -1  1;
      1  1  1;
     -1  1  1;
     -1 -1 -1;
      1 -1 -1;
      1  1 -1;
     -1  1 -1];

F = [1 2 3 4;
      2 6 7 3;
      6 5 8 7;
      5 1 4 8;
      4 3 7 8;
      5 6 2 1];
  
C = [1 0 0;
      0 1 0;
      0 0 1;
      1 1 0;
      1 0 1;
      0 1 1];

% rotations about z and y
ang1 = 90;
ang2 = 90;

c1 = cosd(ang1);
s1 = sind(ang1);

c2 = cosd(ang2);
s2 = sind(ang2);

Rz = [c1 -s1  0;
      s1  c1  0;
      0   0   1];
Ry = [c2  0  s2;
      0   1   0;
     -s2  0  c2];
 
 
% Rz followed by Rz
X = (Ry * Rz * V')';
figure(1); clf
patch('Faces', F, 'Vertices', X, 'FaceVertexCData', C, 'FaceColor', 'flat');
axis([-2 2 -2 2 -2 2])
axis square
xlabel('x')
ylabel('y')
zlabel('z')
title('Rz followed by Ry')

% Ry followed by Rz
X = (Rz * Ry * V')';
figure(2); clf
patch('Faces', F, 'Vertices', X, 'FaceVertexCData', C, 'FaceColor', 'flat');
axis([-2 2 -2 2 -2 2])
axis square
xlabel('x')
ylabel('y')
zlabel('z')
title('Ry followed by Rz')


%% Question 7

A = [1 2 3 4; 5 6 7 8];

fliplr(flipud(A))


%% Question8

A = [1 2 3 4; 5 6 7 8; 9 10 11 12];

reshape(A, [6 2])
