function KegelschnitteS

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

close all
global ax 
ax=[inf -inf inf -inf]; 
deg=pi/180;
p=1;
ex=[0 0.6 1 1.2];
hold('on')
for k=1:length(ex)
    if ex(k)<1
        x=linspace(0,2*p/(1-ex(k)^2),100);
    elseif ex(k)==1
        x=linspace(0,2*ax(2),400);
    else
        x=linspace(0,ax(2),400);
    end
    y=sqrt(2*p*x-(1-ex(k)^2)*x.^2);
  y(find(imag(y)))=NaN;
  drawPolygon(@plot,[x; y],'k')
  drawPolygon(@plot,[x;-y],'k')
  switch k
    case 1
      Schrift([2*p;0],{'\epsilon=0: ' 'Kreis '},'HorizontalAlignment','right','VerticalAlignment','middle');
    case 2
      xR=0.9*x(end);
      Schrift([xR;2*p*xR-(1-ex(k)^2)*xR.^2],{'  0<\epsilon<1:' '  Ellipse'},...
        'HorizontalAlignment','left','VerticalAlignment','bottom');
      b=max(y);
    case 3
      Schrift([x(end);y(end)],{'\epsilon=1:' 'Parabel'},...
        'HorizontalAlignment','left','VerticalAlignment','top');
  end
end
ax(3:4)=[-1 1]*(2*ax(4)+b)/3;
Schrift([max(roots([-1+ex(end)^2 2*p -ax(4)^2]));ax(4)],'\epsilon>1: Hyperbel_{       }',...
  'HorizontalAlignment','right','VerticalAlignment','top');
h=1/2;
ax(1)=ax(1)-h;
ax(2)=ax(2)+2.5*h;
plot(ax(1:2),[0 0],'k')
plot([0 0],ax(3:4),'k')
Spitze([ax(2);0],[1;0],h,30*deg,'k');
Spitze([0;ax(4)],[0;1],h,30*deg,'k');
Schrift([ax(2);0],'\itx','HorizontalAlignment','right','VerticalAlignment','top')
Schrift([0;ax(4)+0.2*h],'\ity ','HorizontalAlignment','right','VerticalAlignment','top')
axis('equal')
axis(ax)
axis('off')
hold('off')
print('-deps','KegelschnitteS');

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