function tetraeder_animated(Tets,KK,maxScale, nanim)
% tetraeder_animated(Tets,KK,maxScale, nanim)
%   
% Tetrahedrons are shown in a dynamic and rotating exploded view
% INPUT:    Test[nelem,4] - element connectivity of the tets
%           KK[3,nnode]   - vertex coordinates
%           maxScale      - maximal expansion factor (0 == do nothing)
%           nanim         - number of dynamic step until the final expansion is reached

maxScale = abs(maxScale);
% Bestimme die Darstellungsgrenzen und setze diese dann mit axis(Plimits)
Pmin = min(KK);
Pmax = max(KK);
MP = mean(KK);

Emin = MP+(1+maxScale)*(Pmin-MP);
Emax = MP+(1+maxScale)*(Pmax-MP);

Plimits = [Emin, Emax];
Plimits([1:2:6 2:2:6]) = Plimits;      % [xmin, xmax, ymin, ymax, zmin, zmax]

% h = figure(1);                % handler for grafics window
h = gcf;                        % handler for grafics window

nelem = size(Tets,1);        % Anzahl der Tetraeder
nnode = size(KK,2);          % Anzahl der Eckpunkte
xlabel('x'),ylabel('y'),zlabel('z')
title(['Explosionsdarstellung: ',num2str(nnode),' Punkte fuer ',num2str(nelem),' Tetraeder'])


% animation
stride = linspace(0,maxScale,nanim);   % Schrittweite bei Expansion
for count=1:numel(stride)              % Teilschritte der Expansion
    tetraeder_explosiv(Tets,KK, stride(count));

    axis equal                         % zuerst
    axis(Plimits)                      %        danach
    xlabel('x'),ylabel('y'),zlabel('z')
    hangle_rot = 360*(count-1)/nanim;  % Rotierende Darstellung im Film
    view([-14 + hangle_rot ,42])
    alpha(0.7)

    F(count) = getframe(h);            % Speichere Grafik als Bild im Film
    hold off
end

disp('... and now the movie')
        % -1   : once forward and backward
movie(h,F,-1,10);               % play movie in Matlab
        %    1 : frames per second  (here, slows down the movie)
% movie(F,10);  % Octave

filename = [mfilename,'.avi'];
v = VideoWriter(filename);
open(v);
writeVideo(v,F);
close(v);
end