function BspDAP

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

close('all')
syms x0 y0 a b alph t real
A=sym('[5;2]');
P=[x0 + a*cos(alph)*cos(t) - b*sin(alph)*sin(t)
   y0 + a*sin(alph)*cos(t) + b*cos(alph)*sin(t)];
deg=sym(pi)/180;
P=subs(P,{a b alph x0 y0},{1.5 1.2 70*deg 2 1})
dP=diff(P,t)
Gl=inline(char(simple(dot(A-P,dP))),'t')
Px=inline(char(P(1)),'t');
Py=inline(char(P(2)),'t');
tRg=[0:360]*double(deg);
plot(Px(tRg),Py(tRg),'k',double(A(1)),double(A(2)),'.k','MarkerSize',30)
hold('on')
AP=zeros(2,0);
d=[];
for k=2:length(tRg)
  if Gl(tRg(k-1))*Gl(tRg(k))<0
    tA=fzero(Gl,[tRg(k-1) tRg(k)]);
    AP(:,end+1)=[Px(tA);Py(tA)];
    d(end+1)=norm(AP(:,end)-double(A));
    plot([double(A(1)) AP(1,end)],[double(A(2)) AP(2,end)],'--k');
  end
end 
[dmin kmin]=min(d);
plot(AP(1,kmin),AP(2,kmin),'ok','MarkerSize',10)
axis('equal')
axis([xlim min(Py(tRg)) max(Py(tRg))]); 
xTck=get(gca,'xTick');
for k=1:length(xTck)
  Schrift([xTck(k);min(ylim)],sprintf('%.1f ',xTck(k)),'Rotation',90,'HorizontalAlignment','right','VerticalAlignment','middle')
end
yTck=get(gca,'yTick');
for k=1:length(yTck)
  Schrift([min(xlim);yTck(k)],sprintf('%.1f ',yTck(k)),'HorizontalAlignment','right','VerticalAlignment','middle')
end
[X,Y]=meshgrid(unique([xlim xTck]),unique([ylim yTck]));
plot(X,Y,X',Y','color',0.5*[1 1 1])
set(gca,'xticklabel',[])
set(gca,'yticklabel',[])
hold('off')
print('-depsc','BspDAP')

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