% normpres.m
% in the 2D-case, a norm-preserving matrix is either a rotation or a reflection.
clear

alpha=pi/3;
c=cos(alpha);
s=sin(alpha);
Rot=[c -s ; s c]
Ref=[c s ; s -c] % Does NOT reflect in the line of slope alpha
x=rand(2,1)-[0.5;0.5];
xrot=Rot*x;
xref=Ref*x;
radius=norm(x);
t=0:0.01:2*pi;
figure(1), clf, hold on, axis equal
plot([0 x(1)],[0 x(2)],'r',x(1),x(2),'rp')
plot([0 xrot(1)],[0 xrot(2)],'g',xrot(1),xrot(2),'gp')
plot([0 xref(1)],[0 xref(2)],'b',xref(1),xref(2),'bp')
plot(radius*cos(t),radius*sin(t),'k')
plot(radius*[-1 1],radius*[-tan(alpha/2) tan(alpha/2)],'b--') % line of reflection
plot([x(1) xref(1)],[x(2) xref(2)],'k-.')

% let us take the projection while we are at it
Proj=(Ref+eye(2))/2
xproj=Proj*x;
plot([0 xproj(1)],[0 xproj(2)],'y',xproj(1),xproj(2),'yp')
title('Original-red, rotation pi/3 -green, reflection in pi/6 -blue, projection-yellow')

% Please find the eigenvalues of Rot, Ref, and Proj.
% Figure out why the are what they are, it is no accident.
% You may find it instructive to vary alpha, then do not trust
% the title of the figure.
% Run this program several times, random data!