# Three-Dimensional Mathematica Graphics (Section 4.1)

### 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.

## 4.1 Primitives and Directives

In previous chapters we have seen how pictures can be made with the built-in functions. We have also seen how the picture which results is a side effect of the evaluation and that there is always a Mathematica expression which is returned. This expression contains a Mathematica representation of the image. If we have a little knowledge of the nature of this representation we can write our own code to generate similar graphics objects, or take existing objects and adapt them in some way. In this section we shall look a little at some of the components of the Mathematica expression which can be used by graphics commands.

### 4.1.1 Primitives

 Circle Disk Line Point Polygon PostScript Raster RasterArray Rectangle Text
Two-dimensional graphics primitives.

 Cuboid Line Point Polygon Text
Three-dimensional graphics primitives.

We can just build expressions containing graphics primitives. The types of coordinates which can be used are discussed in detail in the next chapter. Here the coordinates have their obvious meaning.

 This builds a Mathematica expression using the graphics primitive Line. In[1]:= Line[{{0, 0, 0}, {1, 1, 0}, {2, 2, 1}}] Out[1]= Line[{{0, 0, 0}, {1, 1, 0}, {2, 2, 2}}] The head of Graphics3D is necessary to label the object as containg three-dimensional primitives. The head for two-dimensional primitives is Graphics. In[2]:= Graphics3D[Line[{{0, 0, 0}, {1, 1, 0}, {2, 2, 1}}]] Out[2]= - Graphics3D - The first argument of Show must be a graphics object. In[3]:= Show[Graphics3D[Line[{{0, 0, 0}, {1, 1, 0}, {2, 2, 1}}]]] Out[3]= - Graphics3D - We do not have to explicitly enter numbers for a primitive. So long as we enter an expression which will evaluate to a valid primitive everything works. In[4]:= Line[Table[{i, i, i^2}, {i, 0, 1, 0.1}]] Out[4]= Line[{{0, 0, 0}, {0.1, 0.1, 0.01}, {0.2, 0.2, 0.04}, {0.3, 0.3, 0.09}, {0.4, 0.4, 0.16}, {0.5, 0.5, 0.25}, {0.6, 0.6, 0.36}, {0.7, 0.7, 0.49}, {0.8, 0.8, 0.64}, {0.9, 0.9, 0.81}, {1., 1., 1.}}] This plots the primitives we just built. In[5]:= Show[Graphics3D[%]] Out[5]= - Graphics3D -

### 4.1.2 Text

The use of text in Mathematica graphics presents various special problems connected with placement and orientation and also with the font which is used to render it.

 TextStyle option to determine the text style used FormatType option to determine the format type used Text[ expr , { x , y }] primitive for text of expr at {x, y} Text[ expr , { x , y }, { xoff , yoff }] text of expr with offset of {xoff, yoff} Text[ expr , { x , y }, { xoff , yoff }, { xvec , yvec }] text of expr with direction {xvec, yvec} StyleForm[ expr , opts ] prints expr using font options opts such as FontFamily, FontSize or FontColor
Tools for working with text and fonts.

### TextStyle

All graphical commands possess the option TextStyle which defaults to the global variable \$TextStyle.

 This shows the value of \$TextStyle. Without any setting a Courier font is used. In[6]:= \$TextStyle Out[6]= {} This changes the font used to render text in this plot to 14 printer's point Helvetica. In[7]:= ParametricPlot3D[{Sin[t], Cos[t], Cos[2 t]}, {t, 0, 2 Pi}, TextStyle -> {FontFamily -> "Helvetica", FontSize -> 14}] Out[7]= - Graphics3D -

To change the font for all graphics alter \$TextStyle.

### Text

To include text into graphical images Mathematica provides the primitive Text. It can be used as a primitive in both Graphics and Graphics3D.

 This shows some text. In[8]:= Show[Graphics3D[{Text["this", {-1, 0, 0}], Text["is",{0, 1, 0}], Text["text", {1, -1, 0}]}, ViewPoint -> {0, -3, 0}]] Out[8]= - Graphics3D - The expression which Text takes does not have to be a string. The option FormatType specifies how to format expressions. By default StandardForm is used. In[9]:= Show[Graphics3D[Text[Sin[p], {0, 0, 0}]]] Out[9]= - Graphics3D -

The position given to Text must be a pair of numbers for Graphics and a triple for Graphics3D. Mathematica will issue a warning message if this is not the case. The offset positions the text in relation to this position and is discussed in The Mathematica Book. The default value of the offset, {0, 0}, centers the string at the given position. The direction is used to rotate the text. It is the vector along which the text will lie.

For some window systems it is very hard to rotate text and consequently it will not appear rotated. When the output is printed to a PostScript printer the text will have the correct orientation.

### StyleForm

StyleForm allows the style for a particular text primitive to be altered from the default style for that graphic.

 This changes the font to 24 printer's points boldface Helvetica and adds some color. In[10]:= Show[Graphics3D[Text[StyleForm["This is my string.", FontFamily->"Helvetica", FontSize->24, FontWeight->"Bold", FontColor->RGBColor[0.5, 0, 0.5], Background->RGBColor[1, 1, 0]], {0, 0, 0}]]] Out[10]= - Graphics3D -

### 4.1.3 Directives

 AbsoluteDashing AbsolutePointSize AbsoluteThickness CMYKColor Dashing GrayLevel Hue RGBColor PointSize Thickness
Two-dimensional graphics directives.

 AbsoluteDashing AbsolutePointSize AbsoluteThickness CMYKColor Dashing EdgeForm FaceForm GrayLevel Hue PointSize RGBColor SurfaceColor Thickness
Three-dimensional graphics directives.
 The directive Thickness changes the rendering of the Line primitive which follows it. In[11]:= Show[Graphics3D[{Thickness[0.01], Line[{{0, 0, 0}, {1, 1, 1}}]}]] Out[11]= - Graphics3D - The directive only affects primitives which follow it. In[12]:= Show[Graphics3D[{Line[{{0, 0, 1}, {1, 1, 2}}], Thickness[0.01], Line[{{0, 0, 0}, {1, 1, 1}}]}]] Out[12]= - Graphics3D - The directive only affects primitives at that level or below. It does not travel back up the tree-structure. In[13]:= Show[Graphics3D[{{Thickness[0.01], Line[{{0, 0, 0}, {1, 1, 1}}]}, Line[{{0, 0, 1}, {1, 1, 2}}]}]] Out[13]= - Graphics3D -

Since we can build graphics objects from primitives we can easily write programs to construct these with the tools provided by Mathematica. This is how Mathematica packages generate their pictures. The idea of programming to produce pictures is explored in more detail in Chapter 6.

### FaceForm and EdgeForm

FaceForm and EdgeForm alter just the edge or the face of a polygon. In addition, FaceForm allows the front and back of the polygon to be described differently.

 EdgeForm[] do not draw polygons edges EdgeForm[ dir ] use the directive or list of directives dir to draw polygons edges FaceForm[ dir ] draw the polygon faces with dir FaceForm[ dirf , dirb ] draw the front face with dirf and back face with dirb
 In[14]:= prims = First[ParametricPlot3D[{Sin[t] Cos[p], Sin[t] Sin[p], Cos[t]}, {t, 0, Pi}, {p, 0, 2 Pi}, PlotPoints -> {10, 12}, DisplayFunction -> Identity]]; This does not draw lines at the polygon edges. In[15]:= Show[Graphics3D[{EdgeForm[], prims}]] Out[15]= - Graphics3D - This makes them thick and gray. In[16]:= Show[Graphics3D[{EdgeForm[{GrayLevel[0.5], Thickness[0.01]}], prims}]] Out[16]= - Graphics3D - This shows the top view of the polygon. In[17]:= Show[Graphics3D[{FaceForm[GrayLevel[0], GrayLevel[0.7]], Polygon[{{0, 0, 0}, {1, 0, 0}, {0, 1, 0}}]}], Lighting -> False] Out[17]= - Graphics3D - This shows the bottom, which has a different color. In[18]:= Show[%, ViewPoint -> {1.3, -2.4, -2.}] Out[18]= - Graphics3D -

### Surface Color

The SurfaceColor directive allows one to construct more complex surface properties. It defines the diffuse and specular reflectivities of polygons.

 SurfaceColor[ difcol ] diffuse reflector of color difcol SurfaceColor[ difcol , speccol ] specular reflector of color speccol SurfaceColor[ difcol , speccol , exp ] specular exponent exp