Legacy Documentation

Mathematica® Teacher's Edition (2002)

This is documentation for an obsolete product.
Current products and services
 Documentation /  Mathematica Teacher's Edition /  The Teacher's Book /  Basic Calculations /  Two-Dimensional Graphics /

9.1 Two-Dimensional Graphics Elements

Basic two-dimensional graphics elements.

Here is a line primitive.

In[1]:= sawline = Line[Table[{n, (-1)^n}, {n, 6}]]

Out[1]=

This shows the line as a two-dimensional graphics object.

In[2]:= sawgraph = Show[ Graphics[sawline] ]

Out[2]=

This redisplays the line with axes added.

In[3]:= Show[ %, Axes -> True ]

Out[3]=

You can combine graphics objects that you have created explicitly from graphics primitives with ones that are produced by functions like Plot.

This produces an ordinary Mathematica TE plot.

In[4]:= Plot[Sin[Pi x], {x, 0, 6}]

Out[4]=

This combines the plot with the sawtooth picture made above.

In[5]:= Show[%, sawgraph]

Out[5]=

You can combine different graphical elements simply by giving them in a list. In two-dimensional graphics, Mathematica TE will render the elements in exactly the order you give them. Later elements are therefore effectively drawn on top of earlier ones.

Here is a list of two Rectangle graphics elements.

In[6]:= {Rectangle[{1, -1}, {2, -0.6}],
Rectangle[{4, .3}, {5, .8}]};

This draws the rectangles on top of the line that was defined above.

In[7]:= Show[ Graphics[ {sawline, %} ]]

Out[7]=

The Polygon graphics primitive takes a list of , coordinates, corresponding to the corners of a polygon. Mathematica TE joins the last corner with the first one, and then fills the resulting area.

Here are the coordinates of the corners of a regular pentagon.

In[8]:= pentagon = Table[{Sin[2 Pi n/5], Cos[2 Pi n/5]}, {n, 5}]

Out[8]=

This displays the pentagon. With the default choice of aspect ratio, the pentagon looks somewhat squashed.

In[9]:= Show[ Graphics[ Polygon[pentagon] ] ]

Out[9]=

This chooses the aspect ratio so that the shape of the pentagon is preserved.

In[10]:= Show[%, AspectRatio -> Automatic]

Out[10]=

Circles and disks.

This shows two circles with radius 2. Setting the option AspectRatio -> Automatic makes the circles come out with their natural aspect ratio.

In[11]:= Show[ Graphics[
{Circle[{0, 0}, 2], Circle[{1, 1}, 2]} ],
AspectRatio -> Automatic ]

Out[11]=

This shows a sequence of disks with progressively larger semi-axes in the direction, and progressively smaller ones in the direction.

In[12]:= Show[ Graphics[
Table[Disk[{3n, 0}, {n/4, 2-n/4}], {n, 4}] ],
AspectRatio -> Automatic ]

Out[12]=

Mathematica TE allows you to generate arcs of circles and segments of ellipses. In both cases, the objects are specified by starting and finishing angles. The angles are measured counterclockwise in radians with zero corresponding to the positive direction. For segments of ellipses, the angles are measured as on a circle that is then transformed by scaling into an ellipse.

This draws a wedge centered at the origin.

In[13]:= Show[ Graphics[ Disk[{0, 0}, 1, {0, 140 Degree}] ],
AspectRatio -> Automatic ]

Out[13]=

Raster-based graphics elements.

Here is a array of values between 0 and 1.

In[14]:= modtab = Table[Mod[i, j] / 3., {i, 4}, {j, 4}]

Out[14]=

This uses the array of values as gray levels in a raster.

In[15]:= Show[ Graphics[ Raster[%] ] ]

Out[15]=

This shows two overlapping copies of the raster.

In[16]:= Show[ Graphics[ {Raster[modtab, {{0, 0}, {2, 2}}],
Raster[modtab, {{1.5, 1.5}, {3, 2}}]} ] ]

Out[16]=

In the default case, Raster always generates an array of gray cells. As described in Section 22.2, you can use the option ColorFunction to apply a "coloring function" to all the cells.
You can also use the graphics primitive RasterArray. While Raster takes an array of values, RasterArray takes an array of Mathematica TE graphics directives. The directives associated with each cell are taken to determine the color of that cell. Typically the directives are chosen from the set GrayLevel, RGBColor or Hue. By using RGBColor and Hue directives, you can create color rasters using RasterArray.