# Three-Dimensional Mathematica Graphics (Chapter 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.

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.

## Graphics Output

### 3.2 Hardcopy Output

This chapter discusses the steps which are carried out when a Mathematica graphic is rendered. It first shows how the plotting commands construct primitives and produce a particular type of graphic. Then we see how the primitives are rendered into PostScript. This is followed with a discussion of the ways in which hardcopy output can be obtained.

## 3.1 Graphics Commands and Objects

We have seen how Mathematica can produce a number of different types of graphical objects. These include two-dimensional graphics, three-dimensional graphics and special types which represent images such as simple surfaces. The special types exit for efficiency reasons.

 Graphics two-dimensional picutres SurfaceGraphics simple three-dimensional surfaces Graphics3D general three-dimensional picutres ContourGraphics contour diagrams DensityGraphics bitmaps GraphicsArray arrays of other pictures Sound sounds
Graphics objects in Mathematica.

Built-in commands are provided to construct all these objects. As examples we have seen how Plot3D and ListPlot3D can produce simple surfaces and how ParametricPlot3D produces more general three-dimensional images. For all the graphical objects listed above there is a number of commands which will generate them.

 Command Object Plot, ParametricPlot, ListPlot Graphics Plot3D, ListPlot3D SurfaceGraphics ContourPlot, ListContourPlot ContourGraphics DensityPlot, ListDensityPlot DensityGraphics ParametricPlot3D Graphics3D Play, ListPlay Sound
Graphics commands and the objects they produce.

Each command works by constructing the appropriate object. For example Plot builds a Graphics object. As an aside one should note the distinction between the generic use of the term graphics and the particular use of the Mathematica command Graphics. The latter is the Mathematica expression which holds two-dimensional graphics.

As can be seen there are many easy ways provided to generate all these different types of pictures. Here we shall look in more detail at a typical command, ParametricPlot3D. We can see the actual result, the Mathematica expression which is returned, by using the Mathematica command InputForm; the drawing of the picture is a side effect of the evaluation which returned this object.

 This draws a picture. In[1]:= ParametricPlot3D[{t, t^2, t^3}, {t, 0, 1}, PlotPoints -> 9] Out[1]= - Graphics3D - This is the result returned to Mathematica by the ParametricPlot3D command. The primitives which are found in a result like this are described in the next chapter. In[2]:= InputForm[%] Out[2]//InputForm= Graphics3D[{Line[{{0., 0., 0.}, {0.125, 0.015625, 0.001953125}}], Line[{{0.125, 0.015625, 0.001953125}, {0.25, 0.0625, 0.015625}}], Line[{{0.25, 0.0625, 0.015625}, {0.375, 0.140625, 0.052734375}}], Line[{{0.375, 0.140625, 0.052734375}, {0.5, 0.25, 0.125}}], Line[{{0.5, 0.25, 0.125}, {0.625, 0.390625, 0.244140625}}], Line[{{0.625, 0.390625, 0.244140625}, {0.75, 0.5625, 0.421875}}], Line[{{0.75, 0.5625, 0.421875}, {0.875, 0.765625, 0.669921875}}], Line[{{0.875, 0.765625, 0.669921875}, {1., 1., 1.}}]}, {PlotRange -> Automatic, DisplayFunction :> \$DisplayFunction, ColorOutput -> Automatic, Axes -> True, PlotLabel -> None, AxesLabel -> None, Ticks -> Automatic, Prolog -> {}, Epilog -> {}, AxesStyle -> Automatic, Background -> Automatic, DefaultColor -> Automatic, DefaultFont :> \$DefaultFont, AspectRatio -> Automatic, ViewPoint -> {1.3, -2.399999999999999, 2.}, Boxed -> True, BoxRatios -> Automatic, Plot3Matrix -> Automatic, Lighting -> True, AmbientLight -> GrayLevel[0.], LightSources -> {{{1., 0., 1.}, RGBColor[1, 0, 0]}, {{1., 1., 1.}, RGBColor[0, 1, 0]}, {{0., 1., 1.}, RGBColor[0, 0, 1]}}, ViewCenter -> Automatic, PlotRegion -> Automatic, ImageSize -> Automatic, TextStyle :> \$TextStyle, FormatType :> \$FormatType, ViewVertical -> {0., 0., 1.}, FaceGrids -> None, Shading -> True, RenderAll -> True, PolygonIntersections -> True, AxesEdge -> Automatic, BoxStyle -> Automatic, SphericalRegion -> False}]
 1. fun[t] is evaluated for t varying from tmin to tmax. 2. These points are placed in line primitives and given a head of Graphics3D. 3. The Graphics3D object is passed to Show, which processes options and calls DisplayFunction which typically causes PostScript to be emitted. 4. The Graphics3D object is returned as the result.
The steps involved in evaluating ParametricPlot3D[fun[t], {t, tmin, tmax}].
 It is Show which actually invokes the PostScript interpreter. We can pass the result back into Show again. In[3]:= Show[%] Out[3]= - Graphics3D -

We have seen some of the different uses of Show such as redisplaying graphics and changing options. Later we will see how it can combine pictures by carrying out appropriate conversions. However maybe the most important use of Show is to call the DisplayFunction.

All the graphics commands have the option DisplayFunction. This defaults to \$DisplayFunction which is set to send the graphics to \$Display. The latter will invoke the PostScript interpreter for the window system your computer is using.
 Default setting of DisplayFunction option. In[4]:= Options[Plot, DisplayFunction] Out[4]= {DisplayFunction :> \$DisplayFunction} Default value of \$DisplayFunction. In[5]:= \$DisplayFunction Out[5]= Display[\$Display, #1]&
 Show[obj1, obj2, ... , opts] combine pictures, process options and call DisplayFunction. Display[stream, object] send PostScript representation of object to stream. Display[stream, object, "format"] send object to stream in the specified format
Commands used to display Mathematica graphics.

Generally one does not want to change the DisplayFunction. However, there are resons why you may wish to do this. You maybe want to write your own renderer and use MathLink. Then if you made the correct assignment to DisplayFunction all the commands like ParametricPlot3D and Show would just work. Another use is to set DisplayFunction to be Identity. This suppresses the production of PostScript. In certain situations this is very useful.

 This produces no pictures. The result can be used in GraphicsArray. In[6]:= Table[Plot[x^n, {x, 0, 2}, DisplayFunction -> Identity], {n, 1, 4, 1}] Out[6]= {- Graphics -, - Graphics -, - Graphics -, - Graphics -}

## 3.2 Hardcopy Output

We have seen how the Mathematica kernel can render a sequence of graphics primitives into PostScript. In this section we examine how to obtain hardcopy output from this stream. This is done by rendering the picutre into PostScript with the command Display. We saw in the last section that by default Display is called automatically by Show. However we can call it directly with a top-level command and maybe send the PostScript somewhere other than to an interpreter. For example we can send it into a file.

 This sends PostScript into the file tmp.ps. Here we use Display directly. In[7]:= Display["tmp.ps", Graphics3D[Line[{{0, 0, 0}, {1, 1, 1}}]]] Out[7]= - Graphics3D - Here are the contents of the file which resulted. Note that it is not standard PostScript. In[8]:= !!tmp.ps %! %%Creator: Mathematica %%AspectRatio: 1.0855 MathPictureStart /Mabs { Mgmatrix idtransform Mtmatrix dtransform } bind def /Mabsadd { Mabs 3 -1 roll add 3 1 roll add exch } bind def %% Graphics3D %%IncludeResource: font Courier %%IncludeFont: Courier /Courier findfont 10 scalefont setfont % Scaling calculations -0.0567234 1.16608 3.33392e-17 1.16608 [ [ 0 0 0 0 ] [ 1 1.0855 0 0 ] ] MathScale % Start of Graphics 1 setlinecap 1 setlinejoin newpath 0 g .25 Mabswid [ ] 0 setdash .08846 .24555 m 0 .80374 L s 0 .80374 m .40037 1.0855 L s .40037 1.0855 m .41799 .58158 L s .41799 .58158 m .08846 .24555 L s .67245 0 m .92713 .41497 L s .92713 .41497 m 1 .94814 L s 1 .94814 m .70644 .58546 L s .70644 .58546 m .67245 0 L s .08846 .24555 m 0 .80374 L s 0 .80374 m .70644 .58546 L s .70644 .58546 m .67245 0 L s .67245 0 m .08846 .24555 L s .41799 .58158 m .92713 .41497 L s .92713 .41497 m 1 .94814 L s 1 .94814 m .40037 1.0855 L s .40037 1.0855 m .41799 .58158 L s 0 0 m 1 0 L 1 1.0855 L 0 1.0855 L closepath clip newpath .5 Mabswid .10855 .26104 m .97657 .93008 L s .25 Mabswid .67245 0 m .92713 .41497 L s .92713 .41497 m 1 .94814 L s 1 .94814 m .70644 .58546 L s .70644 .58546 m .67245 0 L s .08846 .24555 m 0 .80374 L s 0 .80374 m .70644 .58546 L s .70644 .58546 m .67245 0 L s .67245 0 m .08846 .24555 L s % End of Graphics MathPictureEnd

Thus Display can send a PostScript form of a Mathematica graphics to a file. However, this PostScript is not suitable to be sent to a printer. This is because PostScript procedure definitions must be added. The methods which are available to work with Mathematica PostScript depend upon whether your front end supports Notebooks or not.

On a Notebook front end it is very easy to print a picture. One simply pulls down the menu item for printing and everything works in the typical way for your particular type of computer. You set up the printer you want and you print. The PostScript header is added automatically.

On front ends which do not support Notebooks the situation is more complex. One of the major reasons for this is that the operating system on Notebook front ends does a large amount of work. On front ends without Notebooks the operating system typically provides fewer facilities. To print a graphic one uses the command PSPrint. This will add header information and send the result to your default printer.

Alternatively, you may wish to make a PostScript file that can be added into some other application program. This involves writing an Encapsulated PostScript file, an EPSF.

 This sends Encapsulated PostScript into the file tmp.eps. Many other formats like GIF, PICT, Metafile, etc. are possible. In[9]:= Display["tmp.eps", Graphics3D[Line[{{0, 0, 0}, {1, 1, 1}}]], "EPS"] Out[9]= - Graphics3D -