function Matr2x2

% http://geometrie.hs-nb.de/MATLAB-Programme/Matr2x2.m   Bearbeitungsstand: 19.10.2004
% (C) Martin Nitschke, nitschke@hs-nb.de

clc;
close('all')
deg=pi/180;
verschieb=inline('P+repmat(v,1,size(P,2))','P','v');
dreh=inline('[cos(t) -sin(t);sin(t) cos(t)]*P','P','t');
spiegel=inline('[cos(t) sin(t);sin(t) -cos(t)]*P','P','t');
nEck=inline('repmat(r,2,1).*[cos(t);sin(t)]','r','t');
ax=[inf -inf inf -inf];
t=linspace(0,2*pi,12);
Q=nEck(3+cos(5.2*t),t);
Q=Q(:,[1:3 5 4 6:end 1])/2;
R(1,:,:)=eye(2);
R(2,:,:)=[ 1.5 -0.5;-0.75 2];
R(3,:,:)=[-1.5 -0.5; 0.75 2];
R(4,:,:)=diag([3 3]/2);
R(5,:,:)=diag([6 6]/10);
R(6,:,:)=diag([3/2 7/10]);
R(7,:,:)=diag([-3/2 7/10]);
R(8,:,:)=[cos(50*deg) -sin(50*deg);sin(50*deg)  cos(50*deg)];
R(9,:,:)=[cos(50*deg)  sin(50*deg);sin(50*deg) -cos(50*deg)];
R(10,:,:)=squeeze(R(8,:,:))*squeeze(R(6,:,:));
R(11,:,:)=squeeze(R(6,:,:))*squeeze(R(8,:,:));
R(12,:,:)=R(1,:,:);
R(13,:,:)=R(8,:,:);
R(14,:,:)=R(6,:,:);
ax=[inf -inf inf -inf];
for k=1:size(R,1)
  P=squeeze(R(k,:,:))*Q;
  ax=[min(ax(1),min(P(1,:))) max(ax(2),max(P(1,:))) min(ax(3),min(P(2,:))) max(ax(4),max(P(2,:)))];
end
h=0.5;
ax=ax+h*[0 1.5 0 0];
for k=1:size(R,1)
  figure('numbertitle','off','name',['Matr2x2-' int2str(k)]);
  A=squeeze(R(k,:,:));
  P=A*Q;
  fill(P(1,:),P(2,:),0.875*[1 1 1])
  hold('on')
  plot(ax(1:2),[0 0],'k')
  plot([0 0],ax(3:4),'k')
  plot([0 1],[0 0],'color',0.5*[1 1 1],'linewidth',2.0)
  plot([0 0],[0 1],'color',0.5*[1 1 1],'linewidth',2.0)
  if k>1
    plot([0 A(1,1)],[0 A(2,1)],'k','linewidth',2.0)
    plot([0 A(1,2)],[0 A(2,2)],'k','linewidth',2.0)
  end
  Spitze([1;0],[1;0],h/2,90*deg,0.5*[1 1 1]);
  Spitze([0;1],[0;1],h/2,90*deg,0.5*[1 1 1]);
  Schrift([ax(2);0],'\itx','HorizontalAlignment','right','VerticalAlignment','bottom');
  Schrift([0;ax(4)+h],'^{ }\ity','HorizontalAlignment','left','VerticalAlignment','top');
  Spitze([ax(2);0],[1;0],h,45*deg,'k');
  Spitze([0;ax(4)],[0;1],h,45*deg,'k');
  switch k
    case 5
      Schrift([1;0],'{\bf\ite}_1','color',0.5*[1 1 1],'HorizontalAlignment','center','VerticalAlignment','top');
      Schrift([0;1],'{\bf\ite}_{2  }','color',0.5*[1 1 1],'HorizontalAlignment','right','VerticalAlignment','middle');
    case 6
      Schrift([1;0],'{\bf\ite}_1','color',0.5*[1 1 1],'HorizontalAlignment','center','VerticalAlignment','bottom');
      Schrift([0;1],'{\bf\ite}_{2  }','color',0.5*[1 1 1],'HorizontalAlignment','right','VerticalAlignment','middle');
    case {8,14}
%       Schrift([1;0],'{\bf\ite}_1','color',0.5*[1 1 1],'HorizontalAlignment','center','VerticalAlignment','top');
%       Schrift([0;1],'{\bf\ite}_2','color',0.5*[1 1 1],'HorizontalAlignment','center','VerticalAlignment','bottom');
    case {9,10}
      Schrift([1;0],'^{ }{\bf\ite}_1','color',0.5*[1 1 1],'HorizontalAlignment','left','VerticalAlignment','middle');
      Schrift([0;1],'{\bf\ite}_2','color',0.5*[1 1 1],'HorizontalAlignment','center','VerticalAlignment','bottom');
    case 11
      Schrift([1;0],'{\bf\ite}_1','color',0.5*[1 1 1],'HorizontalAlignment','center','VerticalAlignment','top');
      Schrift([0;1],'{\bf\ite}_2','color',0.5*[1 1 1],'HorizontalAlignment','center','VerticalAlignment','bottom');
    otherwise
      Schrift([1;0],'{\bf\ite}_1','color',0.5*[1 1 1],'HorizontalAlignment','center','VerticalAlignment','bottom');
      Schrift([0;1],'_{ }{\bf\ite}_2','color',0.5*[1 1 1],'HorizontalAlignment','left','VerticalAlignment','middle');
  end
  if k>1
    Spitze(A(:,1),A(:,1),h/2,90*deg,'k');
    Spitze(A(:,2),A(:,2),h/2,90*deg,'k');
    switch k
      case 2
        Schrift(A(:,1),'{\bf\ita}_1','HorizontalAlignment','center','VerticalAlignment','top');
        Schrift(A(:,2),'{\bf\ita}_2','HorizontalAlignment','center','VerticalAlignment','bottom');
      case 3
        Schrift(A(:,1),'{\bf\ita}_1','HorizontalAlignment','center','VerticalAlignment','bottom');
        Schrift(A(:,2),'{\bf\ita}_2','HorizontalAlignment','center','VerticalAlignment','bottom');
      case 4
        Schrift(A(:,1),'{\bf\ita}_1','HorizontalAlignment','center','VerticalAlignment','bottom');
        Schrift(A(:,2),'_{ }{\bf\ita}_2','HorizontalAlignment','left','VerticalAlignment','middle');
      case 5
        Schrift(A(:,1),'{\bf\ita}_1','HorizontalAlignment','center','VerticalAlignment','top');
        Schrift(A(:,2),'{\bf\ita}_2^{    }','HorizontalAlignment','right','VerticalAlignment','middle');
      case 7
        Schrift(A(:,1),'{\bf\ita}_1','HorizontalAlignment','right','VerticalAlignment','bottom');
        Schrift(A(:,2),'{\bf\ita}_{2  }','HorizontalAlignment','right','VerticalAlignment','middle');
      case {8,13}
        m=tan(atan2(A(2,1),A(1,1)));
        w=atan(m);
        r=0.8*ax(2);
        Kreisbogen([r;0],[0;0],r*[cos(w);sin(w)],0,'k');
        Kreisbogen(r*[cos(w);sin(w)],[0;0],[0;r],0,'--k');
        Kreisbogen([0;r],[0;0],r*[cos(w+pi/2);sin(w+pi/2)],0,'k');
        switch k
          case 8
            Kreisbogen(r*[cos(w+pi/2);sin(w+pi/2)],[0;0],[r;0],0,'--k');
          case 13
            Kreisbogen(r*[cos(w+pi/2);sin(w+pi/2)],[0;0],[-r;0],0,'--k');
            Kreisbogen([0;-r],[0;0],[r;0],0,'--k');
            Schrift(A(:,1),'_{ }{\bf\ita}_1','HorizontalAlignment','left','VerticalAlignment','middle');
            Schrift(A(:,2),'{\bf\ita}_2','HorizontalAlignment','right','VerticalAlignment','bottom');
        end
        plot(A(1,1)*[1 1.1*r],A(2,1)*[1 1.1*r],'k')
        plot(A(1,2)*[1 1.1*r],A(2,2)*[1 1.1*r],'k')
        Schrift(r*[cos(w/2);sin(w/2)],'\alpha ','Rotation',0.5*w/deg,...
          'HorizontalAlignment','right','VerticalAlignment','middle');
        Schrift(r*[cos(pi/2+w/2);sin(pi/2+w/2)],'\alpha','Rotation',0.5*w/deg,...
          'HorizontalAlignment','center','VerticalAlignment','top');
        Spitze(r*A(:,1), A(:,2),h,45*deg,'k');
        Spitze(r*A(:,2),-A(:,1),h,45*deg,'k');
      case 9
        Schrift(A(:,1),'_{ }{\bf\ita}_1','HorizontalAlignment','left','VerticalAlignment','middle');
        Schrift(A(:,2),'^{ }{\bf\ita}_2','HorizontalAlignment','left','VerticalAlignment','middle');
        m=tan(atan2(A(2,1),A(1,1))/2);
        plot(ax(1:2),m*ax(1:2),'-.k');
        plot([A(1,1) 1],[A(2,1) 0],'--k');
        plot([A(1,2) 0],[A(2,2) 1],'--k');
        w=atan(m);
        Schrift([ax(2);m*ax(2)],{'Spiegel-' 'gerade'},'Rotation',w/deg,...
          'HorizontalAlignment','right','VerticalAlignment','bottom');
        r=0.8*ax(2);
        Kreisbogen([r;0],[0;0],r*[cos(w);sin(w)],0,'k');
        Schrift(r*[cos(w/2);sin(w/2)],'\alpha/2','Rotation',0.5*w/deg,...
          'HorizontalAlignment','right','VerticalAlignment','middle');
      case 10
        m=tan(atan2(A(2,1),A(1,1)));
        w=atan(m);
        r=0.83*ax(2);
        Kreisbogen([r;0],[0;0],r*[cos(w);sin(w)],0,'k');
        Kreisbogen(r*[cos(w);sin(w)],[0;0],[0;r],0,'--k');
        Kreisbogen([0;r],[0;0],r*[cos(w+pi/2);sin(w+pi/2)],0,'k');
        Kreisbogen(r*[cos(w+pi/2);sin(w+pi/2)],[0;0],[r;0],0,'--k');
        a1=A(:,1)/norm(A(:,1));
        a2=A(:,2)/norm(A(:,2));
        plot(a1(1)*[norm(A(:,1)) 1.1*r],a1(2)*[norm(A(:,1)) 1.1*r],'k')
        plot(a2(1)*[norm(A(:,2)) 1.1*r],a2(2)*[norm(A(:,2)) 1.1*r],'k')
        Schrift(A(:,1),'_{ }{\bf\ita}_1','HorizontalAlignment','left','VerticalAlignment','middle');
        Schrift(A(:,2),'{\bf\ita}_2','HorizontalAlignment','right','VerticalAlignment','bottom');
        Schrift(r*[cos(w/2);sin(w/2)],'\alpha ','Rotation',0.5*w/deg,...
          'HorizontalAlignment','right','VerticalAlignment','middle');
        Schrift(r*[cos(pi/2+w/2);sin(pi/2+w/2)],'\alpha','Rotation',0.5*w/deg,...
          'HorizontalAlignment','center','VerticalAlignment','top');
        Spitze(r*a1, a2,h,45*deg,'k');
        Spitze(r*a2,-a1,h,45*deg,'k');
      case 11
        Schrift(A(:,1),'_{ }{\bf\ita}_1','HorizontalAlignment','left','VerticalAlignment','middle');
        Schrift(A(:,2),'{\bf\ita}_2','HorizontalAlignment','right','VerticalAlignment','bottom');
      case 14
      otherwise
        Schrift(A(:,1),'{\bf\ita}_1','HorizontalAlignment','left','VerticalAlignment','bottom');
        Schrift(A(:,2),'{\bf\ita}_{2 }','HorizontalAlignment','right','VerticalAlignment','top');
    end
  end
  hold('off')
  axis('off')
  axis('equal')
  switch k
    case {9,12,13}
      axis([-2.4 ax(2) -1.9 ax(4)])
    otherwise
      axis(ax)
  end
  print('-depsc',['Matr2x2-' int2str(k)]);
end

function Schrift(P,txt,varargin)
text(P(1),P(2),txt,'FontName','TimesNewRoman','FontSize',30,varargin{:});

function Spitze(P,v,L,phi,varargin);
if nargin<=2
  L=1;
end
if nargin<=3
  phi=7.5*deg;
end
ta=tan(phi/2);
v=L*v./repmat(sqrt(dot(v,v)),size(P,1),1);
n=[-v(2,:);v(1,:)];
Q=P-v+ta*n;
R=P-v-ta*n;
for k=1:size(P,2);
  fill([P(1,k) Q(1,k) R(1,k)],[P(2,k) Q(2,k) R(2,k)],varargin{:})
end

function Kreisbogen(A,M,B,offset,varargin)
deg=pi/180;
[t1,r1]=cart2pol(A(1)-M(1),A(2)-M(2));
[t2,r2]=cart2pol(B(1)-M(1),B(2)-M(2));
if abs(r1-r2)>sqrt(eps)
  error('Kreisbogen: A und B liegen nicht auf einem Kreis mit Mittelpunkt M')
end
r=(r1+r2)/2;
t1=t1-offset;
t2=t2+offset;
if t2<t1
  t2=t2+2*pi;
end
t=linspace(t1,t2,round((t2-t1)/deg));
P=repmat(M,size(t))+r*[cos(t);sin(t)];
plot(P(1,:),P(2,:),varargin{:});
