function Ortskreis

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

clc;
global ax
ax=[inf -inf inf -inf];
Konstr(inline('','dummy1'));
Konstr(@fig2eps);

function fig2eps(Name)
global ax
axis('off')
axis('equal')
axis(ax)
print('-depsc','-loose',Name)

function Konstr(pr)
close('all')
deg=pi/180;
t0=70*deg;
s=4;
A=[3;5];
B=A+s*[cos(t0);sin(t0)];
Ro=Rot(A,B);
Name=['Ortskreis-Vgb'];
figure('numbertitle','off','name',Name);
drawPolygon(@plot,[A B],'k','linewidth',1.5);
Schrift(A,'\itA','FontSize',60,'Rotation',Ro-90,'HorizontalAlignment','center','VerticalAlignment','top');
Schrift(B,'\itB','FontSize',60,'Rotation',Ro-90,'HorizontalAlignment','center','VerticalAlignment','bottom');
feval(pr,Name);
for alpha=[70 90 110]*deg
  Name=sprintf('Ortskreis-%d',round(alpha/deg));
  figure('numbertitle','off','name',Name);
  hold('on')
  drawPolygon(@plot,[A B],'k','linewidth',1.5);
  Schrift(A,'\itA','FontSize',30,'Rotation',Ro-90,'HorizontalAlignment','center','VerticalAlignment','top');
  Schrift(B,'\itB','FontSize',30,'Rotation',Ro-90,'HorizontalAlignment','center','VerticalAlignment','bottom');
  if abs(alpha-90*deg)<sqrt(eps)
    drawCircle(@plot,A,(A+B)/2,B,90*deg,'k');
  else
    tA=t0+90*deg-alpha;
    tB=t0+90*deg+alpha;
    M1=SchnittGerGer(A,A+[cos(tA);sin(tA)],B,B+[cos(tB);sin(tB)]);
    M2=SchnittGerGer(A,A+[cos(tB);sin(tB)],B,B+[cos(tA);sin(tA)]);
    drawPolygon(@plot,[A A+1.1*(M1-A)],'color',0.5*[1 1 1])
    drawPolygon(@plot,[B B+1.1*(M1-B)],'color',0.5*[1 1 1])
    drawPolygon(@plot,[A A+1.1*(M2-A)],'color',0.5*[1 1 1])
    drawPolygon(@plot,[B B+1.1*(M2-B)],'color',0.5*[1 1 1])
    drawPolygon(@plot,[M1 M2],'--','color',0.5*[1 1 1])
    v=(M1-M2)/2;
    drawPolygon(@plot,[A-v A+v],'--','color',0.5*[1 1 1])
    drawPolygon(@plot,[B-v B+v],'--','color',0.5*[1 1 1])
    if alpha<90*deg
      Schrift(A,'\it\alpha','FontSize',25,'Rotation',Ro,'HorizontalAlignment','left','VerticalAlignment','top');
      Schrift(A,'\it\alpha','FontSize',25,'Rotation',Ro,'HorizontalAlignment','left','VerticalAlignment','bottom');
      Schrift(B,'\it\alpha','FontSize',25,'Rotation',Ro,'HorizontalAlignment','right','VerticalAlignment','top');
      Schrift(B,'\it\alpha','FontSize',25,'Rotation',Ro,'HorizontalAlignment','right','VerticalAlignment','bottom');
      Schrift(M1,'\it_{ }\alpha','FontSize',25,'Rotation',Ro,'HorizontalAlignment','left','VerticalAlignment','top');
      Schrift(M1,'\it\alpha_{ }','FontSize',25,'Rotation',Ro,'HorizontalAlignment','right','VerticalAlignment','top');
      Schrift(M2,'\it_{ }\alpha','FontSize',25,'Rotation',Ro,'HorizontalAlignment','left','VerticalAlignment','bottom');
      Schrift(M2,'\it\alpha_{ }','FontSize',25,'Rotation',Ro,'HorizontalAlignment','right','VerticalAlignment','bottom');
      Schrift(M1,'{\itC}_1','FontSize',30,'Rotation',Ro-90,'HorizontalAlignment','right','VerticalAlignment','middle');
      Schrift(M2,'{\itC}_2','FontSize',30,'Rotation',Ro-90,'HorizontalAlignment','left','VerticalAlignment','middle');
    elseif alpha>90*deg
      Schrift(A,'\it\alpha','FontSize',25,'Rotation',Ro,'HorizontalAlignment','left','VerticalAlignment','top');
      Schrift(A,'\it\alpha','FontSize',25,'Rotation',Ro,'HorizontalAlignment','left','VerticalAlignment','bottom');
      Schrift(B,'\it\alpha','FontSize',25,'Rotation',Ro,'HorizontalAlignment','right','VerticalAlignment','top');
      Schrift(B,'\it\alpha','FontSize',25,'Rotation',Ro,'HorizontalAlignment','right','VerticalAlignment','bottom');
      Schrift(M2,'\it_{ }\alpha','FontSize',25,'Rotation',Ro,'HorizontalAlignment','left','VerticalAlignment','top');
      Schrift(M2,'\it\alpha_{ }','FontSize',25,'Rotation',Ro,'HorizontalAlignment','right','VerticalAlignment','top');
      Schrift(M1,'\it_{ }\alpha','FontSize',25,'Rotation',Ro,'HorizontalAlignment','left','VerticalAlignment','bottom');
      Schrift(M1,'\it\alpha_{ }','FontSize',25,'Rotation',Ro,'HorizontalAlignment','right','VerticalAlignment','bottom');
      Schrift(M2,'{\itC}_1','FontSize',30,'Rotation',Ro-90,'HorizontalAlignment','right','VerticalAlignment','middle');
      Schrift(M1,'{\itC}_2','FontSize',30,'Rotation',Ro-90,'HorizontalAlignment','left','VerticalAlignment','middle');
    end
    feval(pr,[Name '-M']);
    drawCircle(@plot,B,M1,A,0,'k','linewidth',1.5);
    drawCircle(@plot,A,M1,B,0,'color',0.5*[1 1 1]);
    drawCircle(@plot,A,M2,B,0,'k','linewidth',1.5);
    drawCircle(@plot,B,M2,A,0,'color',0.5*[1 1 1]);
  end
  feval(pr,Name);
  hold('off')
end

function S=SchnittGerGer(A,B,C,D)
syms('t','real');
n=[C(2)-D(2);D(1)-C(1)];
S=A+double(solve(n'*(A+t*(B-A)-C),t))*(B-A);

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{:});

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
