function Thales

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

clc;
close('all')
global ax
ax=[inf -inf inf -inf];
deg=pi/180;
t=[0:360]*deg;
t0=180*deg;
t1=t0+180*deg;
K=[cos(t);sin(t)];
drawPolygon(@plot,K,'k')
Schrift( [1;0],'\itB','FontSize',26,'HorizontalAlignment','left');
Schrift(-[1;0],'\itA','FontSize',26,'HorizontalAlignment','right');
hold('on')

q=0.2;

for t2=[60 85 103  -90  -115]*deg;
  P=[0 cos([t1 t2 t0]) 0
    0 sin([t1 t2 t0]) 0];
  drawPolygon(@plot,P,'k','linewidth',1.5)
  v=P(:,4)-P(:,3);
  w=P(:,2)-P(:,3);
  if t2>0
    drawCircle(@plot,P(:,3)+q*v/norm(v),P(:,3),P(:,3)+q*w/norm(w),0,'k');
    Schrift((1-q)*P(:,3),'90^\circ','Rotation',Rot(P(:,3)+q*w/norm(w),P(:,3)+q*v/norm(v)),'HorizontalAlignment','center','VerticalAlignment','bottom')
  else
    drawCircle(@plot,P(:,3)+q*w/norm(w),P(:,3),P(:,3)+q*v/norm(v),0,'k');
    Schrift((1-q)*P(:,3),'90^{\circ  }','Rotation',Rot(P(:,3)+q*w/norm(w),P(:,3)+q*v/norm(v)),'HorizontalAlignment','center','VerticalAlignment','top')
  end
end

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

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',18,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
