function Drehmatrix3d

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

close('all')
a=[1;-2;-3];
a=a/norm(a);
R=[a null(a')];
hold('on');
Achsen;
plot3([0 1],[0 0],[0 0],'color',0.5*[1 1 1],'linewidth',2.0)
plot3([0 0],[0 1],[0 0],'color',0.5*[1 1 1],'linewidth',2.0)
plot3([0 0],[0 0],[0 1],'color',0.5*[1 1 1],'linewidth',2.0)
Spitze([1;0;0],[1;0;0],1/7,40*pi/180,'color',0.5*[1 1 1]);
Spitze([0;1;0],[0;1;0],1/7,40*pi/180,'color',0.5*[1 1 1]);
Spitze([0;0;1],[0;0;1],1/7,40*pi/180,'color',0.5*[1 1 1]);
plot3([0 R(1,1)],[0 R(2,1)],[0 R(3,1)],'k','linewidth',2.0)
plot3([0 R(1,2)],[0 R(2,2)],[0 R(3,2)],'k','linewidth',2.0)
plot3([0 R(1,3)],[0 R(2,3)],[0 R(3,3)],'k','linewidth',2.0)
Spitze(R(:,1),R(:,1),1/7,40*pi/180,'k');
Spitze(R(:,2),R(:,2),1/7,40*pi/180,'k');
Spitze(R(:,3),R(:,3),1/7,40*pi/180,'k');
for j=1:3
  Quader(R(:,j));
end
plot3([0 0],[0 0],[0 R(3,1)],'k')

Schrift([0.8;0;0],'{\bf\ite}_1','color',0.5*[1 1 1],'HorizontalAlignment','left','VerticalAlignment','top');
Schrift([0;0.8;0],'_ { }{\bf\ite}_2','color',0.5*[1 1 1],'HorizontalAlignment','left','VerticalAlignment','bottom');
Schrift([0;0;1],'{\bf\ite}_{3  }','color',0.5*[1 1 1],'HorizontalAlignment','right','VerticalAlignment','middle');
Schrift(R(:,1),'{\bf\ite}_1''','HorizontalAlignment','center','VerticalAlignment','top');
Schrift(R(:,2),'{\bf\ite}_2''','HorizontalAlignment','center','VerticalAlignment','top');
Schrift(R(:,3),'{\bf\ite}_3''','HorizontalAlignment','right','VerticalAlignment','middle');

Schrift([0.5*R(1,1);0; R(3,1)],'{\itr}_{11}','HorizontalAlignment','left','VerticalAlignment','top')
Schrift([R(1,1); 0.5*R(2,1); R(3,1)],'{\itr}_{21}','HorizontalAlignment','center','VerticalAlignment','top')
Schrift([R(1,1); R(2,1); 0.6*R(3,1)],'{\itr}_{31}','HorizontalAlignment','right','VerticalAlignment','middle')

Schrift([0.5*R(1,2); R(2,2); R(3,2)],'{\itr}_{12}','HorizontalAlignment','left','VerticalAlignment','top')
Schrift([0; 0.7*R(2,2); R(3,2)],'{\itr}_{22}','HorizontalAlignment','center','VerticalAlignment','bottom')
Schrift([0; R(2,2); 0.5*R(3,2)],'{\itr}_{32}','HorizontalAlignment','left','VerticalAlignment','middle')

Schrift([0.5*R(1,3); R(2,3); R(3,3)],'{\itr}_{13}','HorizontalAlignment','center','VerticalAlignment','bottom')
Schrift([0; 0.5*R(2,3); R(3,3)],'{\itr}_{23}','HorizontalAlignment','center','VerticalAlignment','bottom')
Schrift([R(1,3); R(2,3); 0.5*R(3,3)],'{\itr}_{33}','HorizontalAlignment','right','VerticalAlignment','middle')
hold('off');
print('-depsc','Drehmatrix3d')

function Quader(P)
plot3([P(1) 0 0 P(1) P(1)],[P(2) P(2) 0 0 P(2)],[P(3) P(3) P(3) P(3) P(3)],'k')
plot3([P(1) 0 0 P(1) P(1)],[P(2) P(2) 0 0 P(2)],[0 0 0 0 0],'k')
plot3([P(1) P(1)],[P(2) P(2)],[P(3) 0],'k')
plot3([P(1) P(1)],[0 0],[P(3) 0],'k')
plot3([0 0],[P(2) P(2)],[P(3) 0],'k')

function Achsen
axis('equal')
axis([-1 1.3 -1 1.5 -0.5 1.4]);
X=xlim;
Y=ylim;
Z=zlim;
plot3([0 X(2)],[0 0],[0 0],'k');
plot3([0 0],[0 Y(2)],[0 0],'k');
plot3([0 0],[0 0],[0 Z(2)],'k');
Schrift([X(2);0;0],'\itx','HorizontalAlignment','right','VerticalAlignment','bottom');
Schrift([0;Y(2);0],'_{ }\ity','HorizontalAlignment','right','VerticalAlignment','bottom');
Schrift([0;0;Z(2)],'{ }\itz');
h=0.2;
w=30*pi/180;
Spitze([X(2);0;0],[1;0;0],h,w,'k');
Spitze([0;Y(2);0],[0;1;0],h,w,'k');
Spitze([0;0;Z(2)],[0;0;1],h,w,'k');
view(133,14);
axis(axis.*[1 1 1 1 0.9 0.8])
axis('off')
grid('on')

function Schrift(P,txt,varargin)
text(P(1),P(2),P(3),txt,'FontName','TimesNewRoman','FontSize',14,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)],...
      varargin{:});
  end
end
