function ViereckInnenwinkel

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

clc;
close('all')
global ax
ax=[inf -inf inf -inf];
deg=pi/180;
alpha=35*deg;
a=9;
b=4;
c=5;
d=10;
A=[0;0];
hold('on')

[B,C1,C2,D]=Abb(A,a,b,c,d,alpha);
BD=norm(D-B);

A(1)=A(1)+1.4*abs(max([B(1) C1(1) C2(1) D(1)])-A(1));
b=0.2*BD;
c=0.7*BD;
[B,C1,C2,D]=Abb(A,a,b,c,d,alpha);

A(1)=A(1)+1.6*abs(max([B(1) C1(1) C2(1) D(1)])-A(1));
b=0.3*BD;
c=0.7*BD;
[B,C1,C2,D]=Abb(A,a,b,c,d,alpha);

hold('off')
axis('equal')
axis(ax)
axis('off')
print('-deps','ViereckInnenwinkel')

function [B,C1,C2,D]=Abb(A,a,b,c,d,alpha)
deg=pi/180;
B=[A(1)+a;A(2)];
D=A+d*[cos(alpha);sin(alpha)];
Polygon([D A B],'k')
K1=struct('M',B,'r',b);
K2=struct('M',D,'r',c);
[C1 C2]=SchnittKreisKreis(K1,K2);
Schrift(A,'\itA','VerticalAlignment','top','HorizontalAlignment','center')
Schrift(B,'\itB','VerticalAlignment','top','HorizontalAlignment','center')
Schrift(D,'\itD','VerticalAlignment','bottom','HorizontalAlignment','center')
Schrift((A+B)/2,'\ita','VerticalAlignment','top','HorizontalAlignment','center','Rotation',Rot(A,B))
Schrift((D+A)/2,'\itd','VerticalAlignment','bottom','HorizontalAlignment','center','Rotation',Rot(D,A))
Schrift(A,'     \alpha','VerticalAlignment','bottom');
if isreal([C1 C2])
  Polygon([B C1 D],'--k')
  Polygon([B C2 D],'--k')
  Schrift((C1+D)/2,'\itc','VerticalAlignment','bottom','HorizontalAlignment','center','Rotation',Rot(C1,D))
  if norm(C1-C2)<sqrt(sqrt(eps))
    Schrift(C1,'{\itC}','VerticalAlignment','bottom','HorizontalAlignment','right')
    Kreisbogen(C2,D,C1,30*deg,'k')
    Kreisbogen(C1,B,C2,30*deg,'k')
    Schrift((B+C1)/2,'\itb','VerticalAlignment','bottom','HorizontalAlignment','center','Rotation',Rot(B,C1))
  else
    Kreisbogen(C2,D,C1,5*deg,'k')
    Kreisbogen(C1,B,C2,5*deg,'k')
    Schrift((B+C1)/2,'\itb','VerticalAlignment','bottom','HorizontalAlignment','center','Rotation',Rot(B,C1))
    Schrift((B+C2)/2,'\itb','VerticalAlignment','bottom','HorizontalAlignment','center','Rotation',Rot(B,C2))
    Schrift((C2+D)/2,'\itc','VerticalAlignment','top','HorizontalAlignment','center','Rotation',Rot(C2,D))
    Schrift(C1,'{\itC}_1','VerticalAlignment','top')
    Schrift(C2,'{\itC}_2','VerticalAlignment','middle','HorizontalAlignment','right')
  end
else
    Kreisbogen(C2,D,C1,50*deg,'k')
    Kreisbogen(C1,B,C2,50*deg,'k')
end

function [S1,S2]=SchnittKreisKreis(K1,K2)
syms('t','real');
P=K2.M+K2.r*[cos(t);sin(t)];
tSol=double(solve((K1.M-P)'*(K1.M-P)-K1.r^2,t));
S1=K2.M+K2.r*[cos(tSol(1));sin(tSol(1))];
if abs(imag(S1))<sqrt(eps)*(1+abs(S1))
  S1=real(S1);
end
S2=K2.M+K2.r*[cos(tSol(2));sin(tSol(2))];
if abs(imag(S2))<sqrt(eps)*(1+abs(S2))
  S2=real(S2);
end

function Schrift(P,txt,varargin)
text(P(1),P(2),txt,'FontName','TimesNewRoman','FontSize',11,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

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

function Polygon(P,varargin);
global ax
plot(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
