function Dreh3d

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

close('all');
v=[1;2;3];
v=v/norm(v);
R=[v null(v')]
the=acos(R(3,3));
phi=atan2(R(3,1),R(3,2));
psi=atan2(R(1,3),-R(2,3));
if the<0
  the=the+2*pi;
end
if phi<0
  phi=phi+2*pi;
end
if psi<0
  psi=psi+2*pi;
end
Test=R-[cos(psi) -sin(psi) 0;sin(psi) cos(psi) 0;0 0 1]*...
  [1 0 0;0 cos(the) -sin(the);0 sin(the) cos(the)]*...
  [cos(phi) -sin(phi) 0;sin(phi) cos(phi) 0;0 0 1]
a=2;
A=[2;0;0];

figure('numbertitle','off','name','Ausgangslage');
R=eye(3);
W=Wuerfel(A,a,R);
hold('on')
plotWuerfel(W);
Achsen;
hold('off')
print('-deps','Dreh3d-0','-zbuffer')

figure('numbertitle','off','name','Drehung 1');
T=degGrid(0,phi);
t=T(end);
w=zeros(length(T),3,8);
for k=1:length(T);
  w(k,:,:)=[cos(T(k)) -sin(T(k)) 0;sin(T(k)) cos(T(k)) 0;0 0 1]*W;
end
W=DrehWuerfel(W,w);
print('-deps','Dreh3d-A','-zbuffer')

figure('numbertitle','off','name','Drehung 2');
T=degGrid(0,the);
t=T(end);
w=zeros(length(T),3,8);
for k=1:length(T);
  w(k,:,:)=[1 0 0;0 cos(T(k)) -sin(T(k));0 sin(T(k)) cos(T(k))]*W;
end
W=DrehWuerfel(W,w);
print('-deps','Dreh3d-B','-zbuffer')

figure('numbertitle','off','name','Drehung 3');
T=degGrid(0,psi);
t=T(end);
w=zeros(length(T),3,8);
for k=1:length(T);
  w(k,:,:)=[cos(T(k)) -sin(T(k)) 0;sin(T(k)) cos(T(k)) 0;0 0 1]*W;
end
W=DrehWuerfel(W,w);
print('-deps','Dreh3d-C','-zbuffer')

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 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); 
fill3([A(1) B(1) C(1) D(1)],[A(2) B(2) C(2) D(2)],[A(3) B(3) C(3) D(3)],0.3*[1 1 1]);
fill3([E(1) F(1) G(1) H(1)],[E(2) F(2) G(2) H(2)],[E(3) F(3) G(3) H(3)],0.4*[1 1 1]);
fill3([A(1) D(1) H(1) E(1)],[A(2) D(2) H(2) E(2)],[A(3) D(3) H(3) E(3)],0.5*[1 1 1]);
fill3([B(1) C(1) G(1) F(1)],[B(2) C(2) G(2) F(2)],[B(3) C(3) G(3) F(3)],0.6*[1 1 1]);
fill3([A(1) B(1) F(1) E(1)],[A(2) B(2) F(2) E(2)],[A(3) B(3) F(3) E(3)],0.7*[1 1 1]);
fill3([D(1) C(1) G(1) H(1)],[D(2) C(2) G(2) H(2)],[D(3) C(3) G(3) H(3)],0.8*[1 1 1]);

function W=DrehWuerfel(V,w);
hold('on')
for k=1:8
  plot3(w(:,1,k),w(:,2,k),w(:,3,k),'k')
  T=(w(end,:,k)-w(end-1,:,k))';
  Spitze([w(end,1,k);w(end,2,k);w(end,3,k)],T,0.7,30*pi/180)
end
plotWuerfel(V);
W=reshape(w(end,:,:),3,8);
plotWuerfel(W);
Achsen;
hold('off')

function Achsen
axis('equal')
axis([-5 5 -5 6 -1 5]);
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','VerticalAlignment','top');
Schrift([0;Y(2);0],'\ity','VerticalAlignment','top','HorizontalAlignment','right');
Schrift([0;0;Z(2)],'{ }\itz');
h=0.6;
w=30*pi/180;
Spitze([X(2);0;0],[1;0;0],h,w);
Spitze([0;Y(2);0],[0;1;0],h,w);
Spitze([0;0;Z(2)],[0;0;1],h,w);
view(149,18);
axis(axis.*[0.7 0.7 0.7 0.7 0 0.7]);
axis('off')
grid('on')

function Schrift(P,txt,varargin)
text(P(1),P(2),P(3),txt,'FontName','TimesNewRoman','FontSize',40,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',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));
