Three-Dimensional Mathematica Graphics (Section 6.3)

Extract from Mathematica Graphics: An Intensive Tutorial

by Tom Wickham-Jones



This HTML document is based on Mathematica Graphics: An Intensive Tutorial by Tom Wickham-Jones. It was adapted by Martin Kraus for non-commercial use.

Mathematica and MathLink are registered trademarks, and MathReader, MathSource and 3-Script are trademarks of Wolfram Research, Inc.

All other product names mentioned are trademarks of their producers.

Copyright 1992 by Wolfram Research, Inc.

All rights reserved. No part of this document may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording or otherwise, without the prior written permission of the copyright holder.



Animation

The animation of Mathematica graphics was introduced in an earlier chapter. The use of the animation package to carry out animations was demonstrated. Here we return to the subject and discuss it in the light of some of the ideas which have been covered in intervening chapters.

<<Graphics`Animation` load the animation package (it may be loaded)
Animate[plot, {t, tmin, tmax}] execute the graphics command plot for a sequence of values of t, and animate the resulting sequence of frames.
ShowAnimation[{g1, g2, ... }] produce an animation from a sequence of graphics objects
Commands defined in the package Graphics`Animation`.
This loads the package. In[42]:= <<Graphics`Animation`
 
Here PlotRange and ViewPoint are set. This is to enhance the effect of the animation. (In this HTML document you have to move the mouse into the picture to start the animation.) In[43]:= Animate[ParametricPlot3D[{t Sin[t+n], t Cos[t + n], t}, {t, 0, 6 Pi}, PlotRange -> {{-20, 20}, {-20, 20}, {0, 20}}, ViewPoint -> {0.5, 1, 3}], {n, 0, 2 Pi - Pi/3, Pi/3}]

As shown here it is very often necessary to give explicit settings to PlotRange. This ensures that objects with the same coordinates do not move from frame to frame. As another example of an animation let us consider a jumping cuboid.

This defines some functions for the calculation. In[44]:= h[t_] := 1.2 + Cos[t];
w[h_] := If[h > 1, 1, Sqrt[1 / h]];
b[h_] := If[h > 1, h - 1, 0];

 
A list is generated which contains a frame index and a pair of points in each element. The points will be two of the corners of our cube. In[45]:= list = N[Table[{t ,{-w[h[t]] / 2, -w[h[t]] / 2, h[t]}, {w[h[t]] / 2, w[h[t]] / 2, b[h[t]]}}, {t, 0, 2 Pi - Pi / 6, Pi / 6}]]

Out[45]= {{0, {-0.5, -0.5, 2.2}, {0.5, 0.5, 1.2}}, {0.523599, {-0.5, -0.5, 2.06603}, {0.5, 0.5, 1.06603}}, {1.0472, {-0.5, -0.5, 1.7}, {0.5, 0.5, 0.7}}, {1.5708, {-0.5, -0.5, 1.2}, {0.5, 0.5, 0.2}}, {2.0944, {-0.597614, -0.597614, 0.7}, {0.597614, 0.597614, 0}}, {2.61799, {-0.865194, -0.865194, 0.333975}, {0.865194, 0.865194, 0}}, {3.14159, {-1.11803, -1.11803, 0.2}, {1.11803, 1.11803, 0}}, {3.66519, {-0.865194, -0.865194, 0.333975}, {0.865194, 0.865194, 0}}, {4.18879, {-0.597614, -0.597614, 0.7}, {0.597614, 0.597614, 0}}, {4.71239, {-0.5, -0.5, 1.2}, {0.5, 0.5, 0.2}}, {5.23599, {-0.5, -0.5, 1.7}, {0.5, 0.5, 0.7}}, {5.75959, {-0.5, -0.5, 2.06603}, {0.5, 0.5, 1.06603}}}
 
This produces a list of graphics. In[46]:= Map[(Graphics3D[{Polygon[{{-1.4, -1.4, 0}, {-1.4, 1.4, 0}, {1.4, 1.4, 0}, {1.4, -1.4, 0}}], Cuboid[#[[2]], #[[3]]]}, ViewPoint -> {2 Sin[#[[1]] / 4], 2 Cos[#[[1]] / 4], 2.}, SphericalRegion -> True, PlotRange -> {{-1.5, 1.5}, {-1.5, 1.5}, {-0.1, 2.3}}])&, list]

Out[46]= {- Graphics3D -, - Graphics3D -, - Graphics3D -, - Graphics3D -, - Graphics3D -, - Graphics3D -, - Graphics3D -, - Graphics3D -, - Graphics3D -, - Graphics3D -, - Graphics3D -, - Graphics3D -}
 
Now we can show the result with the command ShowAnimation. In[47]:= ShowAnimation[%]

For animations of the ViewPoint like this one you may wish to use the option SphericalRegion -> True. This maintains the same scaling as the object is rotated.


next page: 6.4 Examples of Graphics Programming back to table of contents