function KoordinatenflGeo

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

clc;
close('all')
deg=pi/180;
h=0.3;

global ax
lam=40*deg;
rho=0.8;
P=[rho*[cos(lam);sin(lam)];0.9];
O=[0;0;0];
ax=[-1 1 -1 1 -1 1]*norm(P);

figure('numbertitle','off','name','lambda=const');
[U1,U2]=meshgrid(linspace(-h,norm(P)-h,100),linspace(0,norm(P),100));
V1=[0;0;1];
V2=[cos(lam);sin(lam);0];
X=U1*V1(1)+U2*V2(1);
Y=U1*V1(2)+U2*V2(2);
Z=U1*V1(3)+U2*V2(3);
surf(X,Y,Z,U2)
c=0.5+0.375*(cos(pi/2*[0:size(X,1)-1]/(size(X,1)-1))).^2;
colormap(repmat(c',1,3));
shading('interp')
hold('on')
drawPolygon(@plot3,[O norm(P)*[P(1:2);0]]/norm(P(1:2)),'k','erase','none')
drawPolygon(@plot3,[[P(1:2);0] P],'k','erase','none')
drawPolygon(@plot3,[O (norm(P)+h)*P/norm(P)],'k','erase','none','linewidth',1.5);
Spitze((norm(P)+h)*P/norm(P),P,h/3,45*deg)
Schrift(P/2,'\itr_{ }','erase','none','HorizontalAlignment','right','VerticalAlignment','middle','erase','xor');
drawPolygon(@plot3,[O [norm(P)+h;0;0]],'k','erase','none','linewidth',1.5);
drawPolygon(@plot3,[O [0;norm(P)+h;0]],'k','erase','none','linewidth',1.5);
drawPolygon(@plot3,[O [0;0;norm(P)-h]],'k','erase','none','linewidth',1.5);
Spitze([norm(P)+h;0;0],[1;0;0],h/3,45*deg);
Spitze([0;norm(P)+h;0],[0;1;0],h/3,45*deg);
Spitze([0;0;norm(P)-h],[0;0;1],h/3,45*deg);
Schrift([norm(P)+h;0;0],'\itx','erase','none','HorizontalAlignment','right','VerticalAlignment','top');
Schrift([0;norm(P)+h;0],'\ity','erase','none','HorizontalAlignment','left','VerticalAlignment','middle');
Schrift([0;0;norm(P)-h/3],'\itz ','erase','none','HorizontalAlignment','right','VerticalAlignment','top');
[xK,yK]=pol2cart(degGrid(0,lam),0.8*rho);
drawPolygon(@plot3,[xK;yK;zeros(size(xK))],'k','erase','none','linewidth',1.5);
Schrift(0.8*rho*[cos(lam/2);sin(lam/2);0],'\it\lambda','erase','none',...
  'HorizontalAlignment','center','VerticalAlignment','top','erase','xor');
phi=asin(P(3)/norm(P));
[xK,zK]=pol2cart(degGrid(0,phi),0.6*rho);
K=[cos(lam) -sin(lam) 0;sin(lam) cos(lam) 0;0 0 1]*[xK;zeros(size(zK));zK];
drawPolygon(@plot3,K,'k','erase','none','linewidth',1.5);
Schrift(0.6*rho*[cos(lam) -sin(lam) 0;sin(lam) cos(lam) 0;0 0 1]*[cos(phi/2);0;sin(phi/2)],...
  '\it_{ }\phi','erase','none','HorizontalAlignment','left','VerticalAlignment','middle','erase','xor');
Spitze(K(:,end),[-sin(phi)*[cos(lam);sin(lam)];cos(phi)],h/3,45*deg)
Schrift(O,'\itO','erase','none','HorizontalAlignment','right','VerticalAlignment','middle','erase','xor');
hold('off')
axis('off')
axis('equal')
view(99,22)
ax=[-1 1 -1 1+h -0.5 0.5]*norm(P);
axis(ax)
print('-deps','KoordinatenflGeo-lam','-opengl');

figure('numbertitle','off','name','phi=const');
phi=asin(P(3)/norm(P));
[Lam,R]=meshgrid([-180:180]*deg,linspace(0,norm(P)+2*h,100));
[X,Y,Z]=sph2cart(Lam,phi,R);
surf(X,Y,Z,Lam)
c=0.5+0.375*(cos(pi*[0:size(Lam,1)-1]/(size(Lam,1)-1))).^2;
colormap(repmat(c',1,3));
shading('interp')
hold('on')
drawPolygon(@plot3,[O norm(P)*[P(1:2);0]]/norm(P(1:2)),'k','erase','none')
drawPolygon(@plot3,[[P(1:2);0] P],'k','erase','none')
drawPolygon(@plot3,[O (norm(P)+h)*P/norm(P)],'k','erase','none','linewidth',1.5);
Spitze((norm(P)+h)*P/norm(P),P,h/3,45*deg)
Schrift(P/2,'\itr_{ }','erase','none','HorizontalAlignment','right','VerticalAlignment','middle','erase','xor');
drawPolygon(@plot3,[O [norm(P)+h;0;0]],'k','erase','none','linewidth',1.5);
drawPolygon(@plot3,[O [0;norm(P)+h;0]],'k','erase','none','linewidth',1.5);
drawPolygon(@plot3,[O [0;0;norm(P)+h]],'k','erase','none','linewidth',1.5);
Spitze([norm(P)+h;0;0],[1;0;0],h/3,45*deg);
Spitze([0;norm(P)+h;0],[0;1;0],h/3,45*deg);
Spitze([0;0;norm(P)+h],[0;0;1],h/3,45*deg);
Schrift([norm(P)+h;0;0],'\itx','erase','none','HorizontalAlignment','right','VerticalAlignment','top');
Schrift([0;norm(P)+h;0],'\ity','erase','none','HorizontalAlignment','left','VerticalAlignment','middle');
Schrift([0;0;norm(P)+h],'\itz','erase','none','HorizontalAlignment','center','VerticalAlignment','bottom');
[xK,yK]=pol2cart(degGrid(0,lam),0.8*rho);
drawPolygon(@plot3,[xK;yK;zeros(size(xK))],'k','erase','none','linewidth',1.5);
Schrift(0.8*rho*[cos(lam/2);sin(lam/2);0],'\it\lambda','erase','none',...
  'HorizontalAlignment','center','VerticalAlignment','top','erase','xor');
Spitze([xK(end);yK(end);0],[-yK(end);xK(end);0],h/3,45*deg);
[xK,zK]=pol2cart(degGrid(0,phi),0.6*rho);
K=[cos(lam) -sin(lam) 0;sin(lam) cos(lam) 0;0 0 1]*[xK;zeros(size(zK));zK];
drawPolygon(@plot3,K,'k','erase','none','linewidth',1.5);
Schrift(0.6*rho*[cos(lam) -sin(lam) 0;sin(lam) cos(lam) 0;0 0 1]*[cos(phi/2);0;sin(phi/2)],...
  '\it_{ }\phi','erase','none','HorizontalAlignment','left','VerticalAlignment','middle','erase','xor');
Schrift(O,'\itO','erase','none','HorizontalAlignment','right','VerticalAlignment','middle','erase','xor');
hold('off')
axis('off')
axis('equal')
view(99,22)
ax=[-1 1 -1 1+h -0.5 1.15]*norm(P);
axis(ax)
print('-deps','KoordinatenflGeo-phi','-opengl');

figure('numbertitle','off','name','r=const');
ax=[-1 1 -1 1 -1 1]*norm(P);
[Lam,Phi]=meshgrid([-180:180]*deg,[-90:90]*deg);
[X,Y,Z]=sph2cart(Lam,Phi,norm(P));
surf(X,Y,Z,Lam)
c=0.5+0.375*(sin(pi*[0:size(Lam,1)-1]/(size(Lam,1)-1))).^2;
colormap(repmat(c',1,3));
shading('interp')
hold('on')
drawPolygon(@plot3,[O norm(P)*[P(1:2);0]]/norm(P(1:2)),'k','erase','none')
drawPolygon(@plot3,[[P(1:2);0] P],'k','erase','none')
drawPolygon(@plot3,[O P],'k','erase','none','linewidth',1.5);
[xK,yK]=pol2cart([0:360]*deg,rho);
drawPolygon(@plot3,[xK;yK;repmat(P(3),size(xK))],'k','erase','none')
[xK,yK]=pol2cart([0:360]*deg,norm(P));
drawPolygon(@plot3,[xK;yK;zeros(size(xK))],'k','erase','none')
Schrift(P/2,'\itr_{ }','erase','none','HorizontalAlignment','right','VerticalAlignment','middle','erase','xor');
drawPolygon(@plot3,[O [norm(P)+h;0;0]],'k','erase','none','linewidth',1.5);
drawPolygon(@plot3,[O [0;norm(P)+h;0]],'k','erase','none','linewidth',1.5);
drawPolygon(@plot3,[O [0;0;norm(P)+h]],'k','erase','none','linewidth',1.5);
Spitze([norm(P)+h;0;0],[1;0;0],h/3,45*deg);
Spitze([0;norm(P)+h;0],[0;1;0],h/3,45*deg);
Spitze([0;0;norm(P)+h],[0;0;1],h/3,45*deg);
Schrift([norm(P)+h;0;0],'\itx','erase','none','HorizontalAlignment','right','VerticalAlignment','top');
Schrift([0;norm(P)+h;0],'\ity','erase','none','HorizontalAlignment','left','VerticalAlignment','middle');
Schrift([0;0;norm(P)+h/3],'\itz ','erase','none','HorizontalAlignment','right','VerticalAlignment','bottom');
[xK,yK]=pol2cart(degGrid(0,lam),0.8*rho);
drawPolygon(@plot3,[xK;yK;zeros(size(xK))],'k','erase','none','linewidth',1.5);
Schrift(0.8*rho*[cos(lam/2);sin(lam/2);0],'\it\lambda','erase','none',...
  'HorizontalAlignment','center','VerticalAlignment','top','erase','xor');
Spitze([xK(end);yK(end);0],[-yK(end);xK(end);0],h/3,45*deg);
[xK,zK]=pol2cart([-90:90]*deg,norm(P));
K=[cos(lam) -sin(lam) 0;sin(lam) cos(lam) 0;0 0 1]*[xK;zeros(size(zK));zK];
drawPolygon(@plot3,K,'k','erase','none');
phi=asin(P(3)/norm(P));
[xK,zK]=pol2cart(degGrid(0,phi),0.6*rho);
K=[cos(lam) -sin(lam) 0;sin(lam) cos(lam) 0;0 0 1]*[xK;zeros(size(zK));zK];
drawPolygon(@plot3,K,'k','erase','none','linewidth',1.5);
Schrift(0.6*rho*[cos(lam) -sin(lam) 0;sin(lam) cos(lam) 0;0 0 1]*[cos(phi/2);0;sin(phi/2)],...
  '\it_{ }\phi','erase','none','HorizontalAlignment','left','VerticalAlignment','middle','erase','xor');
Spitze(K(:,end),[-sin(phi)*[cos(lam);sin(lam)];cos(phi)],h/3,45*deg)
Schrift(O,'\itO','erase','none','HorizontalAlignment','right','VerticalAlignment','middle','erase','xor');
hold('off')
axis('off')
axis('equal')
view(99,22)
ax=[-1 1 -1 1+h -1 1]*norm(P);
axis(ax)
print('-deps','KoordinatenflGeo-r','-opengl');

function drawPolygon(draw,P,varargin);
global ax
feval(draw,P(1,:),P(2,:),P(3,:),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
if min(P(3,:))<ax(5)
  ax(5)=min(P(3,:));
end
if max(P(3,:))>ax(6)
  ax(6)=max(P(3,:));
end

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

function Spitze(P,v,L,lambda,varargin);
deg=pi/180;
if nargin<=2
  L=1;
end
if nargin<=3
  lambda=7.5*deg;
end
v=L*v./repmat(sqrt(dot(v,v)),size(P,1),1);
Q=P-v;
t=[0:6:360]*deg;
for k=1:size(P,2)
  N=tan(lambda/2)*null(v(:,k)');
  for w=2:length(t)
    plot3(...
      [P(1,k) Q(1,k)+cos(t(w))*L(:,k)*N(1,1)+sin(t(w))*L(:,k)*N(1,2) Q(1,k)+cos(t(w-1))*L(:,k)*N(1,1)+sin(t(w-1))*L(:,k)*N(1,2) P(1,k)],...
      [P(2,k) Q(2,k)+cos(t(w))*L(:,k)*N(2,1)+sin(t(w))*L(:,k)*N(2,2) Q(2,k)+cos(t(w-1))*L(:,k)*N(2,1)+sin(t(w-1))*L(:,k)*N(2,2) P(2,k)],...
      [P(3,k) Q(3,k)+cos(t(w))*L(:,k)*N(3,1)+sin(t(w))*L(:,k)*N(3,2) Q(3,k)+cos(t(w-1))*L(:,k)*N(3,1)+sin(t(w-1))*L(:,k)*N(3,2) P(3,k)],...
      'k','erase','none',varargin{:});
    end
  end

function t=degGrid(t1,t2,d);
if nargin<3
  d=1;
end
deg=pi/180;
delta=d*deg;
t=linspace(t1,t2,round((t2-t1)/delta));

