function PythagorasA

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

clc;
close('all')
global ax
ax=[inf -inf inf -inf];
deg=pi/180;

a=4;
b=3;
c=sqrt(a^2+b^2);
hold('on')

A=[0;0]; B=A+[a;0]; C=A+[0;b];
Abb(A,B,C,[0 1/2 1 0],[0 sqrt(3)/2 0 0]);

A(1)=ax(2)+0.8*a; B=A+[a;0]; C=A+[0;b];
Abb(A,B,C,[0 1 1 0 0],[0 0 1 1 0]);

A3=[ax(2)-0.3*a;-0.5*a]; A=A3; B=A+[a;0]; C=A+[0;b];
t=-linspace(0,pi,4);
X=cos(t);
Y=sin(t);
d=(X(end)-X(1));
X=(X-X(1))/d;
Y=(Y-Y(1))/d;
Abb(A,B,C,X,Y);

A4=[ax(2)+0.4*a;A3(2)]; A=A4; B=A+[a;0]; C=A+[0;b];
t=-linspace(0,pi,5);
X=cos(t);
Y=sin(t);
d=(X(end)-X(1));
X=(X-X(1))/d;
Y=(Y-Y(1))/d;
Abb(A,B,C,X,Y);

A=[A3(1)+0.4*a;1.1*a]; B=A+[a;0]; C=A+[0;b];
t=-[0:180]*deg;
X=cos(t);
Y=sin(t);
d=(X(end)-X(1));
X=(X-X(1))/d;
Y=(Y-Y(1))/d;
Abb(A,B,C,X,Y);

A(1)=A4(1)+0.6*a; B=A+[a;0]; C=A+[0;b];
t=-[0:180]*deg;
X=(1+cos(13*t)/3).*cos(t);
Y=(1+cos(17*t)/3).*sin(t);
d=(X(end)-X(1));
X=(X-X(1))/d;
Y=(Y-Y(1))/d;
Abb(A,B,C,X,Y);

hold('off')
axis('off')
axis('equal')
axis(ax)
print('-depsc','PythagorasA')

function Abb(A,B,C,X,Y);
Fl(B,A,X,Y,'{\itA}_1','\ita',0.875*[1 1 1])
Fl(C,B,X,Y,'{\itA}_3','\itc',0.875*[1 1 1])
Fl(A,C,X,Y,'{\itA}_2','\itb',0.875*[1 1 1])
drawPolygon(@plot,[A B C A],'k')

function Fl(A,B,X,Y,txt1,txt2,varargin)
if (X(1)==X(end) & Y(1)==Y(end))
  X(end)=[];
  Y(end)=[];
end
verschieb=inline('P+repmat(v,1,size(P,2))','P','v');
[t,r]=cart2pol(B(1)-A(1),B(2)-A(2));
Q=verschieb(r*[cos(t) -sin(t);sin(t) cos(t)]*[X;Y],A);
drawPolygon(@fill,Q,varargin{:},'EdgeColor','none');
a=Rot(A,B);
Schrift(mean(Q')',txt1,'HorizontalAlignment','center','VerticalAlignment','middle','Rotation',a)
if a 
  if strcmp(txt2,'\itc')
    Schrift((A+B)/2,txt2,'HorizontalAlignment','center','VerticalAlignment','bottom','Rotation',a)
  else
    Schrift((A+B)/2,txt2,'HorizontalAlignment','center','VerticalAlignment','top','Rotation',a)
  end
else
  Schrift((A+B)/2,txt2,'HorizontalAlignment','center','VerticalAlignment','bottom')
end

function drawPolygon(draw,P,varargin);
global ax
feval(draw,P(1,:),P(2,:),varargin{:})
if min(P(1,:))<ax(1)
  ax(1)=min(P(1,:));
end
if max(P(1,:))>ax(2)
  ax(2)=max(P(1,:));
end
if min(P(2,:))<ax(3)
  ax(3)=min(P(2,:));
end
if max(P(2,:))>ax(4)
  ax(4)=max(P(2,:));
end

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

function r=Rot(a,b)
deg=pi/180;
r=atan2(b(2)-a(2),b(1)-a(1))/deg;
if r<0
  r=r+180;
end
if r>90
  r=r-180;
end
