2. Working with Graphics Objects and Primitives
Graphics Primitives
Graphics primitives are simple geometric items which are often used to built more complex shapes. In Mathematica the primitives must be converted to a Graphics[] respectively Graphics3D[] object for visualization with the Show[] command.
Point
The primitive Point[{x,y}] respectively Point[{x,y,z}] represents a single point.
A point may have a diameter and color. The size and color attributes apply to all points following the attribute setting.
Three vertices of a planar triangle. | In[1]:= pt = Graphics[{PointSize[0.1], Point[{3, 0}],
Point[{1, 0.3}], RGBColor[0, 1, 0], Point[{2, 1}]}];
In[2]:= JavaView[pt] |
Compare with Mathematica |
Three vertices of a triangle in 3D. | In[1]:= pt3D = Graphics3D[{PointSize[0.1], Point[{3, 0, 0}],
Point[{1, 0.3, 1}], RGBColor[0, 1, 0], Point[{2, 1, 0}]}];
In[2]:= JavaView[pt3D] |
Compare with Mathematica |
Generate dots on a function curve from a set of points. | In[1]:= ptc = Graphics[Table[Point[{x, 2Sin[1/x]}], {x, -Pi,
Pi, .01}], Axes -> Automatic]
In[2]:= JavaView[ptc] |
Compare with Mathematica |
Line
The primitive Line[{x1,y1},{x2,y2}] respectively Line[{x1,y1,z1},{x2,y2,z2}] connects two or more points with one or more straight line segments.
A line may have a thickness and color. The thickness and color attributes apply to all segments of a line.
Three lines form a
planar triangle.
|
In[1]:= ln = Graphics[{Thickness[0.01], Line[{{3, 0}, {1,
0.3}, {2, 1}}], RGBColor[1, 0, 0], Line[{{2, 1}, {3, 0}}]}];
In[2]:= JavaView[ln] |
Compare with Mathematica |
Use Line[] to generate a function curve as polygon. | In[1]:= lnc = Graphics[{RGBColor[1, 0.7, 0], Line[Table[{x,
2Sin[1/x]}, {x, -Pi, Pi, .01}]]}, Axes -> Automatic];
In[2]:= JavaView[lnc] |
Compare with Mathematica |
Rectangle
The primitive Rectangle[{x1,y1},{x2,y2}] represents a filled axis-parallel rectangle.
A rectangle may have a color. The color attribute applies to all rectangles following the attribute setting.
Two overlapping
colored rectangles in the plane.
|
In[1]:= rct = Graphics[{Rectangle[{0, 0}, {5, 1}], Hue[.3],
Rectangle[{3, 0}, {4, 2}]}];
In[2]:= JavaView[rct] |
Compare with Mathematica |
Polygon
Polygon[{x1,y1},{x2,y2},...] respectively Polygon[{x1,y1,z1},{x2,y2,z2},...] represents a closed filled polygon where the last vertex is automatically joined with the first. Polygons are a basic primitive to describe surfaces.
In 3D polygons are affected by lighting. A polygon has a front and back side where the orientation is determined by the ordering of the vertices.
Three vertices of a planar triangle shown with default color black. | In[1]:= pol = Graphics[{Polygon[{{3, 0}, {1, 0.3}, {2,
1}}]}]; In[2]:= Show[pol, AspectRatio->Automatic] In[3]:= JavaView[pol] |
Compare with Mathematica |
Two polygons, one of them has green surface color. | In[1]:= wedge = Graphics3D[{Polygon[{{0, 0, 0}, {0, 1, 0},
{0, 1, 1}, {0, 0, 1}}], {SurfaceColor[RGBColor[0, 1, 0]], Polygon[{{0, 0,
0}, {1, 0, 0}, {1, 1, 0}, {0, 1, 0}}]}}];
In[2]:= JavaView[wedge] |
Compare with Mathematica |
Cuboid
The primitive Cuboid[{x,y,z}] represents a square block in 3D with edge size 1 and given lower left corner. A cubical with different size can be generated by specifying the upper right corner too.
Cuboids are handled as polygons by Mathematica and rendered with default lighting.
Several cuboids, and additional cuboid with different surface color. | In[1]:= cub = Graphics3D[{Cuboid[{0, 0, 0}], Cuboid[{0, 0,
1}], Cuboid[{0, 1, 1}], Cuboid[{1, 1, 1}], {SurfaceColor[RGBColor[0, 1,
0]], Cuboid[{1.25, 0.25, 0.25}, {1.75, 0.75, 0.75}]}}];
In[2]:= JavaView[cub] |
Compare with Mathematica |
Circle
The Circle[{x,y}, r] primitive is a circle with given radius, or with more arguments it describes a circle arc or an ellipse. The command is similar to the command Disk[ ].
A circle with given radius. | In[1]:= cs = Graphics[Circle[{1, 1}, 1.5], AspectRatio ->
Automatic, Axes -> Automatic];
In[2]:= JavaView[cs] |
Compare with Mathematica |
A sequence of circles with increasing radius. | In[1]:= a = 0.07; In[2]:= csp = Graphics[Table[Circle[Exp[a x]{Cos[x], Sin[x]}, Exp[a x]/4], {x, 0, 10Pi, .5}], AspectRatio -> Automatic]; In[3]:= JavaView[csp] |
Compare with Mathematica |
The three different Circle[] commands in action. | In[1]:= cc = Graphics[{Circle[{0, 0}, 1], Circle[{0, 0}, {2,
1}], Circle[{0, 0}, 2, {0, 1}]}, AspectRatio -> Automatic, Axes ->
Automatic];
In[2]:= JavaView[cc] |
Compare with Mathematica |
Disk
The Disk[{x,y}, r] primitive is a filled disk with given radius, or with more arguments it describes a filled circle arc or a filled ellipse. The command is similar to Circle[].
A disk with given radius and color. | In[1]:= ds = Graphics[{RGBColor[0, 1, 0], Disk[{1, 1}, 1.5]},
AspectRatio -> Automatic, Axes -> Automatic];
In[2]:= JavaView[ds] |
Compare with Mathematica |
A sequence of disks with increasing radius. | In[1]:= a = 0.07; len = 10 Pi; In[2]:= dsp = Graphics[ Table[{Hue[x/len], Disk[Exp[a x]{Cos[x], Sin[x]}, Exp[a x]/4]}, {x, 0, len, .5}], AspectRatio -> Automatic]; In[3]:= JavaView[dsp] |
Compare with Mathematica |
The three different Disk[] commands in action. | In[1]:= dc = Graphics[{{RGBColor[0, 0, 1], Disk[{0, 0}, {2,
1}]}, {RGBColor[0, 1, 1], Disk[{0, 0}, 1]}, {RGBColor[1, 1, 0], Disk[{0,
0}, 2, {0, 1}]}}, AspectRatio -> Automatic, Axes ->
Automatic];
In[2]:= JavaView[dc] |
Compare with Mathematica |
Raster
Raster[] produces an array with grayscale cells. Optionally, the xy-range of the rectangle can be specified.
JavaView converts a Raster to a colored planar xy-mesh.
Generate a grayscale image as rectangular grid. | In[1]:= rs = Graphics[Raster[Table[.1Mod[i, j], {i, 10}, {j,
10}]]];
In[2]:= JavaView[rs] |
Compare with Mathematica |
RasterArray
RasterArray[] produces an array with colored cells. Optionally, the xy-range of the rectangle can be specified.
JavaView converts a RasterArray to a colored planar xy-mesh.
Generate a color image as rectangular grid. | In[1]:= rsc = Graphics[RasterArray[Table[Hue[.1*Mod[i, j]],
{i, 2, 10}, {j, 2, 10}]], AspectRatio -> Automatic];
In[2]:= JavaView[rsc] |
Compare with Mathematica |
PostScript
The PostScript[] command allows the inclusion of native PostScript methods into a graphics.
A line rendered with
PostScript.
Probably the command will never be supported by JavaView. |
In[1]:= ps = Graphics[{PostScript[".005 setlinewidth"], PostScript[".1 .1 moveto"], PostScript["1.1 .6 -.1 .6 .9 .1 curveto stroke"]}, Frame -> True]; |
Text
The primitive Text["string",{x,y}] and Text["string",{x,y,z}] shows the string centered at the given position. Optionally, an offset can be used to change the alignment in 2D.
Different options exist to change the color, alignment, direction, font etc of a text. JavaView currently supports neither of the additional options.
Four strings around a center point. | In[1]:= txt = Graphics[{Text["Left", {-1, 0}, {-1, 0}],
{RGBColor[0, 1, 0], Text["Right", {1, 0}, {1, 0}]}, Text["Above", {0, 1},
{0, -1}], Text["Below", {0, -1}, {0, 1}], {PointSize[.075], RGBColor[1, 0,
0], Point[{0, 0}]}}, PlotRange -> All];
In[2]:= JavaView[txt] |
Compare with Mathematica |
Labels in 3D. | In[1]:= txt3D = Graphics3D[{Text["LeftUp", {-1, 0, 0.5}],
Text["RightUp", {1, 0, 0.5}], Text["BackLow", {0, 1, -0.5}],
Text["FrontLow", {0, -1, -0.5}], {PointSize[.075], RGBColor[1, 0, 0],
Point[{0, 0, 0}]}}, PlotRange -> All];
In[2]:= JavaView[txt3D] |
Compare with Mathematica |
Arrow
Arrow[{x1,y1},{x2,y2}] specifies base and end point of an arrow in the plane or in space. Different options allow to change the size and appearance of the arrow head.
Arrows are converted to a point set with vectors in JavaView. Currently, individual coloring of vectors is not enabled in JavaView.
Two arrows in different colors. | In[1]:= << Graphics`Arrow` In[2]:= arr = Graphics[{Arrow[{0, 0}, {1, 1}], Hue[0], Arrow[{.75, .25}, {.25, .75}]}]; In[3]:= JavaView[arr] |
Compare with Mathematica |
You can even place arrows in options. | In[1]:= << Graphics`Arrow` In[2]:= pa = Plot[2 Sin[x], {x, 0, 2Pi}, Epilog -> {Arrow[{4, .25}, {Pi/2, 1}], Text["Here", {4, .15}, {0, -1}]}]; In[3]:= JavaView[pa] |
Compare with Mathematica |
Spline
The Spline[{{x1,y1},..}] primitive describes a cubic polynomial, a Bezier or a composite Bezier curve based on a set of given control points.
Splines will soon be enabled in JavaView. Currently JavaView renders the polygon only.
This is a graph of
the cubic Spline primitive combined with other primitives. Here a line of
a different color connects the points making up the spline.
Partially supported only. |
In[1]:= << Graphics`Spline` In[2]:= pts = {{0, 0}, {1, 2}, {-1, 3}, {0, 1}, {3, 0}}; In[3]:= spl = Graphics[{Spline[pts, Cubic], Hue[0], Line[pts]}]; In[4]:= JavaView[spl] |
Compare with Mathematica |
ContourGraphics Object
ContourPlot[] is used to produce level lines. The command generates a ContourGraphics[] objects containing a rectangular array of scalar values. The contour lines itself are produced in a subsequent Show[] command or by a conversion to a Graphics[] object. The command produces both flat faces and the level curves.
Before being passed to JavaView a ContourGraphics will be casted to Graphics in Mathematica. The received Graphics object consists of Lines[] as contour lines and of Polygons[] as level regions. The first polygon is a rectangle of GrayLevel 0.5 filling the whole display. It is followed by polygons in falling and ascending order of GrayLevel, each bright polygon is part of each darker polygon surrounding it down to GrayLevel 0.5, and each dark polygon is part of each brighter polygon surrounding it up to GrayLevel 0.5.
In the JavaView display SORTING must be disabled in order to keep the original order of faces. All faces are in the same plane and would otherwise not correctly ordered.
This gives a
contour plot of the function sin(x)sin(y).
Assure that SORTING is disabled in the JavaView display inspector. |
In[1]:= c = ContourPlot[Sin[x] Sin[y], {x, -2, 2}, {y, -2,
2}]
In[2]:= JavaView[c] |
Compare with Mathematica |
Switches off the shading avoids the problem of overlapping faces in JavaView. | In[1]:= gcns = ContourPlot[Sin[x] Sin[y], {x, -2, 2}, {y, -2,
2}, ContourShading -> False]
In[2]:= JavaView[gcns] |
Compare with Mathematica |
Switches off the contours and views just the shading. | In[1]:= gcnl = ContourPlot[Sin[x] Sin[y], {x, -2, 2}, {y, -2,
2}, ContourLines -> False]
In[2]:= JavaView[gcnl] |
Compare with Mathematica |
DensityGraphics Object
DensityGraphics[] shows a the values of a scalar function over a rectangular array of points in the plane as grayscale image. Lighter regions have higher values.
Density plot of an array of scalars. | In[1]:= gd = DensityPlot[Sin[x] Sin[y], {x, -2, 2}, {y, -2,
2}]
In[2]:= JavaView[gd] |
Compare with Mathematica |
Suppresses the grid lines. | In[1]:= gdnm = DensityPlot[Sin[x] Sin[y], {x, -2, 2}, {y, -2,
2}, Mesh -> False]
In[2]:= JavaView[gdnm] |
Compare with Mathematica |
SurfaceGraphics Object
The Plot3D[] command evaluates a function over a rectangular domain and shows the graph as SurfaceGraphics.
Show the graph of a function over a rectangular planar domain. | In[1]:= plt = Plot3D[Sin[x y], {x, 0, 4}, {y, 0, 4}];
In[2]:= JavaView[plt] |
Compare with Mathematica |
The SurfaceGraphics[] command takes a height field of z-coordinates to visualize a graph over a rectangular xy-domain with given size. Note, the size of the domain must be specified explicitly using the option MeshRange, otherwise Mathematica puts grid values for x and y to the values 1, 2, 3 ...
JavaView parses the height field and automatically calculates the mesh based on the size of the domain and the number of grid points.
Use the option MeshRange to show the x and y coordinates at that values where the z coordinates are computed. | In[1]:= sgM = SurfaceGraphics[ Table[Sin[x]*Cos[y], {x, -Pi,
Pi, Pi/4.}, {y, -Pi, Pi, Pi/4.}], MeshRange -> {{-Pi, Pi}, {-Pi, Pi}},
Axes -> True];
In[2]:= JavaView[sgM] |
Compare with Mathematica |
A SurfaceGraphics object where MeshRange is not defined. Note the wrong tick values. | In[1]:= sg = SurfaceGraphics[Table[Sin[x]*Cos[y], {x, -Pi,
Pi, Pi/4.}, {y, -Pi, Pi, Pi/4.}], Axes -> True];
In[2]:= JavaView[sg] |
Compare with Mathematica |
ListPlot3D[array] generates a three-dimensional plot of a surface representing an array of height values. ListPlot3D[array, shades] generates a plot with each element of the surface shaded according to the specification in shades.
Here is a 3D plot made from a perturbed list of data. | In[1]:= lp = ListPlot3D[ Table[5 (Sin[x*y] + Random[Real,
{-0.15, 0.15}]), {x, 0, 3 Pi/2, Pi/15}, {y, 0, 3 Pi/2, Pi/15}]]
In[2]:= JavaView[lp] |
Compare with Mathematica |
GraphicsArray Object
Several graphics objects can be combined into a single image using GraphicsArray[].
Take a simple
surface graphics and draw its height as grayscale image.
Combine the surface and the height image into a single image. Partially supported only. |
In[1]:= gp = Plot3D[Sin[x] Sin[y], {x, -2, 2}, {y, -2, 2}] In[2]:= dp = DensityPlot[Sin[x] Sin[y], {x, -2, 2}, {y, -2, 2}] In[3]:= ga = GraphicsArray[{gp, dp}]; In[4]:= JavaView[ga] |
Compare with Mathematica |
This generates a
list of graphics objects. Setting DisplayFunction -> Identity stops
Plot3D from rendering the graphics it produces. Explicitly setting
PlotRange ensures that the scale is the same in each piece of graphics.
The command GraphicsArray[] partitions the graphics into three rows, and shows the resulting array of images. Partially supported only. |
In[1]:= table = Table[ Plot3D[BesselJ[0, Sqrt[x2 + y2] + t],
{x, -10, 10}, {y, -10, 10}, Axes -> False, PlotRange -> {-0.5, 1.0},
DisplayFunction -> Identity], {t, 0, 8}] In[2]:= gt = GraphicsArray[Partition[table, 3]]; In[3]:= JavaView[gt] |
Compare with Mathematica |