%% See the relationship between vector position and the matrix determinant in R^2
% 
% This m-file produces an animation, and requires the m-file PlotVector.m


%% Begin with the standard basis
A = eye(2);

% j = 1 : Rotatations from 0 to 2pi
% j = 2 : Collapse slowly onto same vector (parallel to (1,1))
% j = 3 : Pass through to a change of orientation of basis

% Save the animation
close all;
%mov = avifile('SeeDeterminant.avi');
f1 = figure(1);
set(f1, 'Position', [0 0 560 420 ] );


%% Do all transformations in sequence
for j = [1 2 3]

		% Rotations
		if j == 1;
				% Standard rotation matrix in R^2
				R = @(t) [cos(t) -sin(t); sin(t) cos(t)]; 
				% Parameter of rotation
				T = [0:.5:2*pi];
				% Axis
				Ax = [-2 2 -2 2];

		% Collapse
		elseif j == 2;
				% Deformation matrix tricks 
				a = @(t) 1*(1-t) + t*sqrt(2)/2;
				s = sqrt(2)/2;
				R = @(t) [a(t) t*s; t*s a(t)];

				% Parameter of deformation
				T = [0:.1:1];
				% Axis
				Ax = [-.3 1.3 -.3 1.3];

		% Pass to orientation reversing
		elseif j == 3;	
				% Deformation matrix tricks 
				s = sqrt(2)/2;
				a = @(t,s) sqrt(2)/2*(1-t) + t;
				R = @(t) [(1-t)*s a(t); a(t) (1-t)*s ];

				% Parameter of deformation
				T = [0:.1:1];
				% Axis
				Ax = [-.3 1.3 -.3 1.3];

		end


%% Call PlotVector for each transformation

		% Plot Settings

		k = PlotVector(A,'.r',Ax);
		m = 1;

		for t = T;

			tie_tull = title(['Determinant = ',num2str(det(R(t)*A))]);
			set(tie_tull,'FontSize',20);

			h = PlotVector(R(t)*A,'.r',Ax);

			% Display matrix and determinant
			Matrix_Vectors = R(t)*A
			Determinant = det(R(t)*A)

			% Pause for discussion
			pause(.25);

			%% Grab frame
  			%frame = getframe(gcf);
			%mov = addframe(mov,frame);

			% Clear plot for next transformation
			delete(h);
		end

end

% Leave the last plot for discussion
h = PlotVector(R(t)*A,'.r',Ax);
%mov = close(mov);

% To see the animation again - in MATLAB type: implay('SeeDeterminant.avi')
