function Matr3x3

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

close('all');
a=1;
O=[0;0;0];
deg=pi/180;
R(1,:,:)=eye(3);
R(2,:,:)=[11 7 -3;7 10 8;13 -11 9]/13;
R(3,:,:)=1.5*eye(3);
R(4,:,:)=0.6*eye(3);
R(5,:,:)=diag([0.7; 1.5;2]);
R(6,:,:)=diag([-0.7; 1.5;2]);
R(7,:,:)=diag([ 0.7;-1.5;2]);
R(8,:,:)=diag([-0.7;-1.5;2]);
t=50*deg;
R(9,:,:)=[cos(t) 0 -sin(t);0 1 0;sin(t) 0 cos(t)];
R(10,:,:)=[cos(t) 0 sin(t);0 1 0;sin(t) 0 -cos(t)];
ax=[inf -inf inf -inf inf -inf];
for k=1:size(R,1)
  P=Wuerfel(O,a,squeeze(R(k,:,:)));
  ax=[min(ax(1),min(P(1,:))) max(ax(2),max(P(1,:)))...
      min(ax(3),min(P(2,:))) max(ax(4),max(P(2,:)))...
      min(ax(5),min(P(3,:))) max(ax(6),max(P(3,:)))];
end
ax=ax.*[1 1.5 1 1.5 1 1.2];
for k=1:size(R,1)
  figure('numbertitle','off','name',['Matr3x3-' int2str(k)]);
  A=squeeze(R(k,:,:));
  W=Wuerfel(O,a,A);
  hold('on')
  plotWuerfel(W);
  Achsen(ax);
  hold('off')
  print('-deps',['Matr3x3-' int2str(k)],'-opengl');
end


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 Achsen(ax)
X=ax(1:2);
Y=ax(3:4);
Z=ax(5:6);
plot3(X,[0 0],[0 0],'k');
plot3([0 0],Y,[0 0],'k');
plot3([0 0],[0 0],Z,'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.3;
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(ax);
axis('equal')
axis('off')

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