function freierVektor3d

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

clc;
close('all')
deg=pi/180;
global ax
ax=[inf -inf inf -inf inf -inf];
a=[2;1.5;2.5];
O=[0;0;0];
P0=[0.8;0.7;0.6];
drawPolygon(@plot3,[P0 P0+a],'k','linewidth',1.5)
hold('on')
Spitze(P0+a,a,0.5,30*deg);
Schrift(P0+a/2,'\bf\ita_{ }','FontSize',36,'HorizontalAlignment','right','VerticalAlignment','middle');
Schrift(P0+[a(1)/2;0;0],'{\ita}_1','HorizontalAlignment','center','VerticalAlignment','top');
Schrift(P0+[a(1);a(2)/2;0],'_{ }{\ita}_2','HorizontalAlignment','left','VerticalAlignment','middle');
Schrift(P0+[a(1);a(2);a(3)/2],'_{ }{\ita}_3','HorizontalAlignment','left','VerticalAlignment','middle');
h=0.25;
drawPolygon(@plot3,[P0 P0+[a(1);0;0] P0+[a(1:2);0] P0+a],'--k')
drawPolygon(@plot3,[P0 P0+[a(1:2);0]],'k')
Spitze(P0+[a(1:2);0],[a(1:2);0],h,45*pi/180)
Schrift(P0+[a(1:2);0]/2,'{\bf\ita}''','HorizontalAlignment','left','VerticalAlignment','middle');
drawPolygon(@plot3,[O [0;P0(2)+a(2)+1.5*h;0]],'k')
drawPolygon(@plot3,[O [P0(1)+a(1)+1.5*h;0;0]],'k')
drawPolygon(@plot3,[O [0;0;P0(1)+a(3)+11*h]],'k')
Spitze([ax(2);0;0],[1;0;0],h,45*pi/180)
Schrift([ax(2);0;0],'\itx','HorizontalAlignment','left','VerticalAlignment','middle');
Spitze([0;ax(4);0],[0;1;0],h,45*pi/180)
Schrift([0;ax(4);0],'\ity','HorizontalAlignment','center','VerticalAlignment','bottom');
Spitze([0;0;ax(6)],[0;0;1],h,45*pi/180)
Schrift([0;0;ax(6)],'\itz','HorizontalAlignment','center','VerticalAlignment','bottom');

hold('off')
axis('off')
view(-25,40)
axis('equal')
axis(ax+0.5*h*[-1 1 -1 1 -1 1])
print('-depsc','freierVektor3d')

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