function AllgEllipse

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

global ax
close('all')
deg=pi/180;
A=[2;1];
h=0.1;
a=1.5;
b=1.2;
alpha=70*deg;
t=[0:360]*deg;
E1=[a*cos(t);b*sin(t)];
E2=[cos(alpha) -sin(alpha);sin(alpha) cos(alpha)]*E1;
E3=repmat(A,1,length(t))+E2;
S1=[-a a;0 0];
S2=[cos(alpha) -sin(alpha);sin(alpha) cos(alpha)]*S1;
S3=[A A]+S2;
N1=[0 0;-b b];
N2=[cos(alpha) -sin(alpha);sin(alpha) cos(alpha)]*N1;
N3=[A A]+N2;
E=[E1 E2 E3];
ax=[min(E(1,:))-h/10 max(E(1,:)) min(E(2,:))-h/10 max(E(2,:))+h/5];

figure('numbertitle','off','name','Ausgangslage');
drawPolygon(@plot,[0 0 ax(2);ax(4) 0 0],'k');
hold('on')
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')
Schrift([a;-h/2],'\ita','HorizontalAlignment','left','VerticalAlignment','bottom')
Schrift([0;b-h/2],'\itb','HorizontalAlignment','left','VerticalAlignment','bottom')
drawPolygon(@plot,S1,'--k','linewidth',2.0)
drawPolygon(@plot,N1,'--k','linewidth',2.0)
drawPolygon(@plot,E1,'k','linewidth',2.0)
hold('off')
axis('off')
axis('equal');
axis(ax);
print('-depsc','-loose','AllgEllipse-1')

figure('numbertitle','off','name','Drehung');
drawPolygon(@plot,[0 0 ax(2);ax(4) 0 0],'k');
hold('on')
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')
Schrift([3*h;0],'\alpha','HorizontalAlignment','left','VerticalAlignment','bottom')
t=0:deg:alpha;
drawPolygon(@plot,8*h*[cos(t);sin(t)],'k')
Spitze(8*h*[cos(alpha);sin(alpha)],[-sin(alpha);cos(alpha)],2*h,30*deg,0.0*[1 1 1]);
drawPolygon(@plot,S1,'--','color',0.5*[1 1 1],'linewidth',2.0)
drawPolygon(@plot,N1,'--','color',0.5*[1 1 1],'linewidth',2.0)
drawPolygon(@plot,E1,'color',0.5*[1 1 1],'linewidth',2.0)
drawPolygon(@plot,E2,'k','linewidth',2.0)
drawPolygon(@plot,S2,'--k','linewidth',2.0)
drawPolygon(@plot,N2,'--k','linewidth',2.0)
hold('off')
axis('off')
axis('equal');
axis(ax);
print('-depsc','-loose','AllgEllipse-2')

figure('numbertitle','off','name','Verschiebung');
drawPolygon(@plot,[0 0 ax(2);ax(4) 0 0],'k');
hold('on')
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')
drawPolygon(@plot,S2,'--','color',0.5*[1 1 1],'linewidth',2.0)
drawPolygon(@plot,N2,'--','color',0.5*[1 1 1],'linewidth',2.0)
drawPolygon(@plot,E2,'color',0.5*[1 1 1],'linewidth',2.0)
drawPolygon(@plot,[[0;0] A],'k')
Spitze(A,A,2*h,30*deg,0.0*[1 1 1]);
Schrift(A-[2*h;h],'{\itP}_0','HorizontalAlignment','left','VerticalAlignment','bottom')
drawPolygon(@plot,E3,'k','linewidth',2.0)
drawPolygon(@plot,S3,'--k','linewidth',2.0)
drawPolygon(@plot,N3,'--k','linewidth',2.0)
hold('off')
axis('off')
axis('equal');
axis(ax);
print('-depsc','-loose','AllgEllipse-3')


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