function KreisEllipseG

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

global ax
close('all')
deg=pi/180;
A=[5;2];
r=2.2;
h=0.1;
a=1.5;
b=1.2;
t1=150*deg;
tRef=65*deg;
ax=[-r-h/5 A(1)+r+3*h -r-h/10 A(2)+r+2.5*h];

figure('numbertitle','off','name','Kreis');
hold('on')
drawPolygon(@plot,[[0;0] A],'k');
drawPolygon(@plot,[r*[cos(tRef);sin(tRef)] A+r*[cos(tRef);sin(tRef)]],'--k');
Schrift([0;0],'{\itO}','HorizontalAlignment','right','VerticalAlignment','top','color',0.5*[1 1 1])
Schrift(A,'{\itP}_0','HorizontalAlignment','left','VerticalAlignment','top')
Schrift(A+r*[cos(tRef);sin(tRef)],'{\it P}','HorizontalAlignment','left','VerticalAlignment','baseline')
Schrift(r*[cos(tRef);sin(tRef)],'{\it P}-{\itP}_0','color',0.5*[1 1 1],'HorizontalAlignment','right','VerticalAlignment','bottom')
Spitze([0;0],-A,3*h,30*deg,0.0*[1 1 1]);
Spitze(r*[cos(tRef);sin(tRef)],-A,3*h,30*deg,0.0*[1 1 1]);
drawPolygon(@plot,[0 0 ax(2);ax(4) 0 0],'k');
Spitze([ax(2);0],[1;0],h,90*deg,0.0*[1 1 1]);
Spitze([0;ax(4)],[0;1],h,90*deg,0.0*[1 1 1]);
Schrift([ax(2);h/2],'\itx','HorizontalAlignment','right','VerticalAlignment','top')
Schrift([0;ax(4)+2*h],'\ity_{ }','HorizontalAlignment','right','VerticalAlignment','top')
t=[0:360]*deg;
drawPolygon(@plot,[[0;0] r*[cos(t1);sin(t1)]],'color',0.5*[1 1 1])
Schrift(0.5*r*[cos(t1);sin(t1)],'\itr','color',0.5*[1 1 1],'Rotation',180+t1/deg,...
    'HorizontalAlignment','center','VerticalAlignment','bottom')
drawPolygon(@plot,r*[cos(t);sin(t)],'color',0.5*[1 1 1],'linewidth',2.0)
drawPolygon(@plot,[A A+r*[cos(t1);sin(t1)]],'k')
Schrift(A+0.5*r*[cos(t1);sin(t1)],'   \itr','Rotation',180+t1/deg,...
    'HorizontalAlignment','center','VerticalAlignment','bottom')
drawPolygon(@plot,repmat(A,1,length(t))+r*[cos(t);sin(t)],'k','linewidth',2.0)
drawPolygon(@plot,A+r*[cos(tRef);sin(tRef)],'ok','MarkerSize',12)
drawPolygon(@plot,r*[cos(tRef);sin(tRef)],'o','color',0.5*[1 1 1],'MarkerSize',12)
hold('off')
axis('off')
axis('equal');
axis(ax);
print('-depsc','-loose','KreisEllipseG-1')

figure('numbertitle','off','name','Ellipse');
ax=[-a-h/5 a+3*h -b-h/10 b+2.5*h];
A=[0;0];
tRef=220*deg;
drawPolygon(@plot,A,'ok','Markersize',10);
Schrift(A,'{\itO}','HorizontalAlignment','right','VerticalAlignment','top')
hold('on')
t=[0:360]*deg;
drawPolygon(@plot,repmat(A,1,length(t))+diag([a,b])*[cos(t);sin(t)],'k','linewidth',2.0)
u=0.45;
drawPolygon(@plot,[A-[0;h/2] A+[u;-h/2]],'color',0.5*[1 1 1],'linewidth',2.0)
drawPolygon(@plot,[A-[h/2;0] A+[-h/2;u]],'color',0.5*[1 1 1],'linewidth',2.0)
drawPolygon(@plot,repmat(A,1,length(t))+u*[cos(t);sin(t)],'color',0.5*[1 1 1],'linewidth',2.0)
drawPolygon(@plot,[A A+[a;0]],'color',0.0*[1 1 1],'linewidth',2.0)
drawPolygon(@plot,[A A+[0;b]],'color',0.0*[1 1 1],'linewidth',2.0)
Spitze([u;-h/2],[1;0],h,90*deg,0.5*[1 1 1]);
Spitze([-h/2;u],[0;1],h,90*deg,0.5*[1 1 1]);
Spitze([a;0],[1;0],h,90*deg,0.0*[1 1 1]);
Spitze([0;b],[0;1],h,90*deg,0.0*[1 1 1]);
Schrift([u-0.9*h;-h/2],'{\bf\ite}_1','color',0.5*[1 1 1],'HorizontalAlignment','center','VerticalAlignment','top');
Schrift([-h/2;u-h/2],'{\bf\ite}_{2  }','color',0.5*[1 1 1],'HorizontalAlignment','right','VerticalAlignment','middle');
Schrift([a+2*h;0],'{\bf\ite}_1''={\ita\bfe}_1','color',0.0*[1 1 1],'HorizontalAlignment','center','VerticalAlignment','bottom');
Schrift([0;b-2*h],'{\bf\ite}_2''={\itb\bfe}_2','color',0.0*[1 1 1],'HorizontalAlignment','left','VerticalAlignment','bottom');
Schrift(u*[cos(tRef);sin(tRef)],'({\itx}/{\ita},{\ity}/{\itb})_{ }',...
  'color',0.5*[1 1 1],'HorizontalAlignment','right','VerticalAlignment','baseline')
Schrift([a*cos(tRef);b*sin(tRef)],'({\itx},{\ity})','HorizontalAlignment','right','VerticalAlignment','top')
drawPolygon(@plot,u*[cos(tRef);sin(tRef)],'o','color',0.5*[1 1 1],'MarkerSize',12)
drawPolygon(@plot,[a*cos(tRef);b*sin(tRef)],'ok','MarkerSize',12)
hold('off')
axis('off')
axis('equal');
axis(ax);
print('-depsc','-loose','KreisEllipseG-2')


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)
text(P(1),P(2),txt,'FontName','TimesNewRoman','FontSize',40,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
