function Moendchen

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

clc;
close('all')
global ax
ax=[inf -inf inf -inf];
J=inline('[-v(2);v(1)]','v');

a=4;
b=3;
c=sqrt(a^2+b^2);
A=[0;0]; B=A+[a;0]; C=A+[0;b];
drawPolygon(@fill,[A B C A],0.875*[1 1 1])
Schrift((A+B+C)/3,'\itA_D','FontSize',24,'HorizontalAlignment','center','VerticalAlignment','middle')
hold('on')

K1=drawCircle(@plot,B,(B+C)/2,C,0,'k');
v=C-B;
Schrift((B+C)/2-J(v)/4,'\itA_H','FontSize',24,'HorizontalAlignment','center','VerticalAlignment','middle')
K2a=drawCircle(@plot,C,(B+C)/2,A,0,'k');
[q k]=min(K2a(1,:));
K1L=K2a(:,k);
Schrift(((C+A)/2+K1L)/2,'{\itA}_{{\itS}1}','Rotation',90,'FontSize',24,'HorizontalAlignment','center','VerticalAlignment','middle')
K2b=drawCircle(@plot,C,(A+C)/2,A,0,'k');
drawPolygon(@fill,[K2a K2b],0.875*[1 1 1]);
[q k]=min(K2b(1,:));
K2L=K2b(:,k);
Schrift((K1L+K2L)/2,'{\itA}_{{\itM}1}','Rotation',90,'FontSize',24,'HorizontalAlignment','center','VerticalAlignment','middle')
K3a=drawCircle(@plot,A,(B+C)/2,B,0,'k');
[q k]=min(K3a(2,:));
K1U=K3a(:,k);
Schrift(((A+B)/2+K1U)/2,'{\itA}_{{\itS}2}','FontSize',24,'HorizontalAlignment','center','VerticalAlignment','middle')
K3b=drawCircle(@plot,A,(A+B)/2,B,0,'k');
drawPolygon(@fill,[K3a K3b],0.875*[1 1 1]);
[q k]=min(K3b(2,:));
K3U=K3b(:,k);
Schrift((K1U+K3U)/2,'{\itA}_{{\itM}2}','FontSize',24,'HorizontalAlignment','center','VerticalAlignment','middle')

hold('off')
axis('off')
axis('equal')
axis(ax+[-0.05 0 -0.05 0])
print('-depsc','Moendchen')

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 C=drawCircle(draw,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));
C=repmat(M,size(t))+r*[cos(t);sin(t)];
drawPolygon(draw,C,varargin{:});

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