function Abstand

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

global ax
close('all');

figure('numbertitle','off','name','Euklidischer Abstand');
ax=[inf -inf inf -inf inf -inf];
hold('on')
A=[0;0;0];
a=2;
Abb(A,a);
drawPolygon(@plot3,[A A+[a;a;a]],'--k','erase','xor','linewidth',2.0)
Schrift(A+[a;a;a]/2,'_{ }{\itd}({\itA},{\itB})','HorizontalAlignment','left','VerticalAlignment','middle','erase','xor');
hold('off')
view(-30,16)
axis('equal')
axis('off')
axis(ax);
print('-depsc','Abstand-Euklidisch')

figure('numbertitle','off','name','Geodätischer Abstand');
ax=[inf -inf inf -inf inf -inf];
hold('on')
A=[0;0;0];
a=2;
col=Abb(A,a);
drawPolygon(@plot3,[A A+[a/2;0;a] A+[a;a;a]],'k','erase','xor','linewidth',2.0)
Schrift(A+[3*a/8;0;3*a/4],'{_{ }\itd_W}({\itA},{\itB})','HorizontalAlignment','left','VerticalAlignment','middle','erase','xor');
hold('off')
view(-30,16)
axis('equal')
axis('off')
axis(ax);
print('-depsc','Abstand-geodaetisch')

figure('numbertitle','off','name','Geodätischer Abstand - Lösung');
ax=[inf -inf ax(3:4) inf -inf];
hold('on')
A=[0;0;0];
a=2;
W=Wuerfel(A,a,eye(3));
B=W(:,2); C=W(:,3); D=W(:,4); E=W(:,5); F=W(:,6); G=W(:,7); H=W(:,8); 
drawPolygon(@fill3,[A B F E],col(5,:),'EdgeColor','none');
drawPolygon(@fill3,[E F F+[0;0;a] E+[0;0;a]],col(2,:),'EdgeColor','none');
Schrift([0;0;0],'\itA','HorizontalAlignment','right','VerticalAlignment','top');
Schrift([a;0;2*a],'\itB','HorizontalAlignment','left','VerticalAlignment','bottom');
Schrift([a;0;a/2],'\ita','HorizontalAlignment','left','VerticalAlignment','middle');
Schrift([a;0;1.5*a],'\ita','HorizontalAlignment','left','VerticalAlignment','middle');
Schrift([a/2;0;0],'\ita','HorizontalAlignment','center','VerticalAlignment','top');
drawPolygon(@plot3,[A A+[a;0;2*a]],'k','erase','xor','linewidth',2.0)
Schrift(A+[3*a/8;0;3*a/4],'{_{ }\itd_W}({\itA},{\itB})','HorizontalAlignment','left','VerticalAlignment','middle','erase','xor');
hold('off')
view(0,0)
axis('equal')
axis('off')
axis(ax);
print('-depsc','Abstand-geodaetisch-Lsg')

function col=Abb(A,a)
global ax
ax=[inf -inf inf -inf inf -inf];
R=eye(3);
O=[0;0;0];
W=Wuerfel(A,a,R);
col=plotWuerfel(W);
h=0.25;
drawPolygon(@plot3,[O [a+2*h;0;0]],'k','erase','xor')
drawPolygon(@plot3,[O [0;a+3*h;0]],'k','erase','xor')
drawPolygon(@plot3,[O [0;0;a+5*h]],'k','erase','xor')
Schrift([0;0;0],'{\itA}=(0,0,0)','HorizontalAlignment','right','VerticalAlignment','top');
Schrift([a;a;a],'{\itB}=({\ita},{\ita},{\ita})','HorizontalAlignment','left','VerticalAlignment','bottom');
Schrift([a;0;a/2],'\ita','HorizontalAlignment','left','VerticalAlignment','top');
Spitze([ax(2);0;0],[1;0;0],h,30*pi/180)
Schrift([ax(2);0;0],'\itx^{ }','HorizontalAlignment','left','VerticalAlignment','middle');
Spitze([0;ax(4);0],[0;1;0],h,30*pi/180)
Schrift([0;ax(4);0],'\ity','HorizontalAlignment','center','VerticalAlignment','bottom');
Spitze([0;0;ax(6)],[0;0;1],h,30*pi/180)
Schrift([0;0;ax(6)+0.5*h],'\itz_{ }','HorizontalAlignment','right','VerticalAlignment','top');

function W=Wuerfel(A,a,R)
B=A+a*R(:,1);
C=B+a*R(:,2);
D=A+a*R(:,2);
E=A+a*R(:,3);
F=B+a*R(:,3);
G=C+a*R(:,3);
H=D+a*R(:,3);
W=[A B C D E F G H];

function col=plotWuerfel(W)
A=W(:,1); B=W(:,2); C=W(:,3); D=W(:,4); E=W(:,5); F=W(:,6); G=W(:,7); H=W(:,8); 
col=repmat(linspace(0.5,0.75,6)',1,3);
drawPolygon(@fill3,[A B C D],col(1,:),'EdgeColor','none');
drawPolygon(@fill3,[E F G H],col(2,:),'EdgeColor','none');
drawPolygon(@fill3,[A D H E],col(3,:),'EdgeColor','none');
drawPolygon(@fill3,[B C G F],col(4,:),'EdgeColor','none');
drawPolygon(@fill3,[A B F E],col(5,:),'EdgeColor','none');
drawPolygon(@fill3,[D C G H],col(6,:),'EdgeColor','none');

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',40,varargin{:});

function Spitze(P,v,L,phi);
deg=pi/180;
if nargin<=2
  L=1;
end
if nargin<=3
  phi=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(phi/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');
  end
end
