function Polkoord3d

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

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

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

figure('numbertitle','off','name','Zylinderkoordinaten');
[T,R]=meshgrid([0:3:360]*deg,0:0.05:1);
[X,Y]=pol2cart(T,R);
surf(X,Y,zeros(size(X)),T);
cmap=0.5+0.375*(cos(pi*[0:size(T,2)-1]/(size(T,2)-1))).^2;
colormap(repmat(cmap',1,3));
shading('interp')
hold('on')
drawPolygon(@plot3,[O [P(1:2);0] P],'k','linewidth',1.5)
Schrift(P,' {\itP}({\itp}_1,{\itp}_2,{\itp}_3)',...
  'HorizontalAlignment','left','VerticalAlignment','middle','erase','xor');
Schrift([P(1:2);0],' {\itP}''({\itp}_1,{\itp}_2,0)',...
  'HorizontalAlignment','left','VerticalAlignment','middle','erase','xor');
Spitze(O,-[cos(lam);sin(lam);0],h,30*deg);
Spitze([P(1:2);0],[cos(lam);sin(lam);0],h,30*deg);
Schrift(0.65*rho*[cos(lam);sin(lam);0]-[0;0;h/3],'   \it\rho',...
  'HorizontalAlignment','center','VerticalAlignment','bottom','erase','xor');
Schrift([rho*[cos(lam);sin(lam)];P(3)/2],' \ith',...
  'HorizontalAlignment','left','VerticalAlignment','middle','erase','xor');
Spitze([P(1:2);0],[0;0;-1],h,30*deg);
Spitze(P,[0;0;1],h,30*deg);
drawPolygon(@plot3,[O [ax(2)+h;0;0]],'k','linewidth',1.5);
drawPolygon(@plot3,[O [0;ax(4)+h;0]],'k','linewidth',1.5);
drawPolygon(@plot3,[O [0;0;ax(6)+h]],'k','linewidth',1.5);
Spitze([ax(2)+h;0;0],[1;0;0],h,45*deg);
Spitze([0;ax(4)+h;0],[0;1;0],h,45*deg);
Spitze([0;0;ax(6)+h],[0;0;1],h,45*deg);
Schrift([ax(2)+h;0;0],'\itx','HorizontalAlignment','right','VerticalAlignment','top');
Schrift([0;ax(4)+h;0],'\ity','HorizontalAlignment','left','VerticalAlignment','baseline');
Schrift([0;0;ax(6)+h],'\itz','HorizontalAlignment','center','VerticalAlignment','bottom');
[xK,yK]=pol2cart(degGrid(0,lam),rho/2);
drawPolygon(@plot3,[xK;yK;zeros(size(xK))],'k','linewidth',1.5);
Schrift(rho/2*[cos(lam/2);sin(lam/2);0],'\it\lambda',...
  'HorizontalAlignment','center','VerticalAlignment','top','erase','xor');
Spitze([xK(end);yK(end);0],[-yK(end);xK(end);0],h,30*deg);
hold('off')
axis('off')
axis('equal')
axis(ax)
view(99,22)
zlim([0 0.85])
print('-deps','Polkoord3d-Zyl','-opengl');

figure('numbertitle','off','name','Geographische Koordinaten');
[T,R]=meshgrid([0:3:360]*deg,0:0.05:1);
[X,Y]=pol2cart(T,R);
surf(X,Y,zeros(size(X)),T);
cmap=0.5+0.375*(cos(pi*[0:size(T,2)-1]/(size(T,2)-1))).^2;
colormap(repmat(cmap',1,3));
shading('interp')
hold('on')
[T,R]=meshgrid([0:3:180]*deg,0:0.05:1);
[XX,Z]=pol2cart(T,R);
X=cos(lam)*XX;
Y=sin(lam)*XX;
surf(X,Y,Z,T);
cmap=0.5+0.375*(cos(pi*[0:size(T,2)-1]/(size(T,2)-1))).^2;
colormap(repmat(cmap',1,3));
shading('interp')
drawPolygon(@plot3,[O [P(1:2);0] P],'k','linewidth',1.5)
drawPolygon(@plot3,[O P],'k','linewidth',1.5);
Schrift(P,' {\itP}({\itp}_1,{\itp}_2,{\itp}_3)',...
  'HorizontalAlignment','left','VerticalAlignment','middle','erase','xor');
Schrift([P(1:2);0],' {\itP}''({\itp}_1,{\itp}_2,0)',...
  'HorizontalAlignment','left','VerticalAlignment','middle','erase','xor');
Spitze(O,-[cos(lam);sin(lam);0],h,30*deg);
Spitze([P(1:2);0],[cos(lam);sin(lam);0],h,30*deg);
Schrift(0.65*rho*[cos(lam);sin(lam);0]-[0;0;h/3],'   \it\rho',...
  'HorizontalAlignment','center','VerticalAlignment','bottom','erase','xor');
Spitze(O,-P,h,30*deg);
Spitze(P,P,h,30*deg);
Schrift(0.6*P,'\itr ','HorizontalAlignment','right','VerticalAlignment','middle','erase','xor');
drawPolygon(@plot3,[O [ax(2)+h;0;0]],'k','linewidth',1.5);
drawPolygon(@plot3,[O [0;ax(4)+h;0]],'k','linewidth',1.5);
drawPolygon(@plot3,[O [0;0;ax(6)+h]],'k','linewidth',1.5);
Spitze([ax(2)+h;0;0],[1;0;0],h,45*deg);
Spitze([0;ax(4)+h;0],[0;1;0],h,45*deg);
Spitze([0;0;ax(6)+h],[0;0;1],h,45*deg);
Schrift([ax(2)+h;0;0],'\itx','HorizontalAlignment','right','VerticalAlignment','top');
Schrift([0;ax(4)+h;0],'\ity','HorizontalAlignment','left','VerticalAlignment','baseline');
Schrift([0;0;ax(6)+h],'\itz','HorizontalAlignment','center','VerticalAlignment','bottom');
[xK,yK]=pol2cart(degGrid(0,lam),rho/2);
drawPolygon(@plot3,[xK;yK;zeros(size(xK))],'k','linewidth',1.5);
Schrift(rho/2*[cos(lam/2);sin(lam/2);0],'\it\lambda',...
  'HorizontalAlignment','center','VerticalAlignment','top','erase','xor');
Spitze([xK(end);yK(end);0],[-yK(end);xK(end);0],h,30*deg);
phi=asin(P(3)/norm(P));
[xK,zK]=pol2cart(degGrid(0,phi),norm(P)/4);
K=[cos(lam) -sin(lam) 0;sin(lam) cos(lam) 0;0 0 1]*[xK;zeros(size(zK));zK];
drawPolygon(@plot3,K,'k','linewidth',1.5);
Schrift(norm(P)/4*[cos(lam) -sin(lam) 0;sin(lam) cos(lam) 0;0 0 1]*[cos(phi/2);0;sin(phi/2)],...
  '\it\phi','HorizontalAlignment','left','VerticalAlignment','middle','erase','xor');
Spitze(K(:,end),[-sin(phi)*[cos(lam);sin(lam)];cos(phi)],h,30*deg)
hold('off')
axis('off')
axis('equal')
axis(ax)
view(99,22)
zlim([0 0.85])
print('-deps','Polkoord3d-Geo','-opengl');

figure('numbertitle','off','name','Kugelkoordinaten');
[T,R]=meshgrid([0:3:360]*deg,0:0.05:1);
[X,Y]=pol2cart(T,R);
surf(X,Y,zeros(size(X)),T);
cmap=0.5+0.375*(cos(pi*[0:size(T,2)-1]/(size(T,2)-1))).^2;
colormap(repmat(cmap',1,3));
shading('interp')
hold('on')
[T,R]=meshgrid([0:3:180]*deg,0:0.05:1);
[XX,Z]=pol2cart(T,R);
X=cos(lam)*XX;
Y=sin(lam)*XX;
surf(X,Y,Z,T);
cmap=0.5+0.375*(cos(pi*[0:size(T,2)-1]/(size(T,2)-1))).^2;
colormap(repmat(cmap',1,3));
shading('interp')
drawPolygon(@plot3,[O [P(1:2);0] P],'k','linewidth',1.5)
drawPolygon(@plot3,[O P],'k','linewidth',1.5);
Schrift(P,' {\itP}({\itp}_1,{\itp}_2,{\itp}_3)',...
  'HorizontalAlignment','left','VerticalAlignment','middle','erase','xor');
Schrift([P(1:2);0],' {\itP}''({\itp}_1,{\itp}_2,0)',...
  'HorizontalAlignment','left','VerticalAlignment','middle','erase','xor');
Spitze(O,-[cos(lam);sin(lam);0],h,30*deg);
Spitze([P(1:2);0],[cos(lam);sin(lam);0],h,30*deg);
Schrift(0.65*rho*[cos(lam);sin(lam);0]-[0;0;h/3],'   \it\rho',...
  'HorizontalAlignment','center','VerticalAlignment','bottom','erase','xor');
Spitze(O,-P,h,30*deg);
Spitze(P,P,h,30*deg);
Schrift(P/3,'\itr','HorizontalAlignment','left','VerticalAlignment','middle','erase','xor');
drawPolygon(@plot3,[O [ax(2)+h;0;0]],'k','linewidth',1.5);
drawPolygon(@plot3,[O [0;ax(4)+h;0]],'k','linewidth',1.5);
drawPolygon(@plot3,[O [0;0;ax(6)+h]],'k','linewidth',1.5);
Spitze([ax(2)+h;0;0],[1;0;0],h,45*deg);
Spitze([0;ax(4)+h;0],[0;1;0],h,45*deg);
Spitze([0;0;ax(6)+h],[0;0;1],h,45*deg);
Schrift([ax(2)+h;0;0],'\itx','HorizontalAlignment','right','VerticalAlignment','top');
Schrift([0;ax(4)+h;0],'\ity','HorizontalAlignment','left','VerticalAlignment','baseline');
Schrift([0;0;ax(6)+h],'\itz','HorizontalAlignment','center','VerticalAlignment','bottom');
[xK,yK]=pol2cart(degGrid(0,lam),rho/2);
drawPolygon(@plot3,[xK;yK;zeros(size(xK))],'k','linewidth',1.5);
Schrift(rho/2*[cos(lam/2);sin(lam/2);0],'\it\lambda',...
  'HorizontalAlignment','center','VerticalAlignment','top','erase','xor');
Spitze([xK(end);yK(end);0],[-yK(end);xK(end);0],h,30*deg);
phi=asin(P(3)/norm(P));
[xK,zK]=pol2cart(degGrid(phi,pi/2),norm(P)/2);
K=[cos(lam) -sin(lam) 0;sin(lam) cos(lam) 0;0 0 1]*[xK;zeros(size(zK));zK];
drawPolygon(@plot3,K,'k','linewidth',1.5);
Schrift(norm(P)/2*[cos(lam) -sin(lam) 0;sin(lam) cos(lam) 0;0 0 1]*[cos((pi/2+phi)/2);0;sin((pi/2+phi)/2)],...
  '\it\theta','HorizontalAlignment','left','VerticalAlignment','bottom','erase','xor');
Spitze(K(:,1),[sin(phi)*[cos(lam);sin(lam)];-cos(phi)],h,30*deg)
hold('off')
axis('off')
axis('equal')
axis(ax)
view(99,22)
zlim([0 0.85])
print('-deps','Polkoord3d-Kug','-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',32,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));

