function Traktrix

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

close all
global ax a xInl dxInl x0
a=5.1;
h=a/5;
ax=[inf -inf  -h -inf];
deg=pi/180;
y=linspace(0.07*a,a,100);
xStr=subs('a*acosh(a/y)-sqrt(a^2-y^2)','a',a);
dxStr=diff(xStr,'y');
xInl=inline(vectorize(xStr),'y');
dxInl=inline(vectorize(dxStr),'y');
drawPolygon(@plot,[ xInl(y);y],'k','linewidth',2.0);
hold('on')
drawPolygon(@plot,[-xInl(y);y],'k','linewidth',2.0);
ax2=ax(2);
x=1.3:1.3:ax(2)+4;
for k=1:length(x)
  x0=x(k);
  yH=fzero(@res,[0.1 a-0.1]);
  H=[xInl(yH);yH];
  A=[x(k);0];
  drawPolygon(@plot,[H A],'color',0.5*[1 1 1])
  drawPolygon(@plot,[H A],'.','MarkerSize',20,'color',0.5*[1 1 1])
  drawPolygon(@plot,[[-H(1);H(2)] [-A(1);A(2)]],'color',0.5*[1 1 1])
  drawPolygon(@plot,[[-H(1);H(2)] [-A(1);A(2)]],'.','MarkerSize',20,'color',0.5*[1 1 1])
  if k <= 4
    Schrift((A+H)/2-0.06*h*[A(2)-H(2);H(1)-A(1)],'\ita','rotation',180+atan2(H(2)-A(2),H(1)-A(1))/deg,'color',0.5*[1 1 1],...
      'HorizontalAlignment','center','VerticalAlignment','top')
  end
end
ax(1:2)=ax2*[-1 1];
plot(0,0,'.','MarkerSize',20,'color',0.5*[1 1 1])
ax(4)=ax(4)+1.125*h;
plot(ax(1:2),[0 0],'k')
plot([0 0],ax(3:4),'k')
Spitze([ax(2);0],[1;0],0.75*h,30*deg,'k');
Spitze([0;ax(4)],[0;1],0.75*h,30*deg,'k');
Schrift([ax(2)+0.2*h;0.2*h],'\itx','HorizontalAlignment','right','VerticalAlignment','top')
Schrift([0.4*h;ax(4)+0.25*h],'\ity','HorizontalAlignment','left','VerticalAlignment','top')
Schrift([0;a-0.3*h],'\ita ','HorizontalAlignment','right','VerticalAlignment','baseline')
axis('equal')
axis(ax)
axis('off')
hold('off')
print('-depsc','Traktrix');

function xA=Hund(yT) % berechnet zur "Hundposition" (xT,yT) die x-Koord. der "Herrchenposition" 
global a xInl dxInl 
xT=xInl(yT);
dx=dxInl(yT);
T=a*[dx;1]/sqrt(dx^2+1);
xA=xT-T(1);

function r=res(yT)
global x0
r=Hund(yT)-x0;

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)
if length(P)==2
  P=[P;0];
end
text(P(1),P(2),P(3),txt,'FontName','TimesNewRoman','FontSize',16,varargin{:});

function Spitze(P,v,L,phi,varargin);
if nargin<=2
  L=1;
end
if nargin<=3
  phi=7.5*pi/180;
end
ta=tan(phi/2);
v=L*v./repmat(sqrt(dot(v,v)),size(P,1),1);
n=[-v(2,:);v(1,:)];
Q=P-v+ta*n;
R=P-v-ta*n;
for k=1:size(P,2);
  fill([P(1,k) Q(1,k) R(1,k)],[P(2,k) Q(2,k) R(2,k)],varargin{:})
end
