WolframLang: Graphics Primitives
Here's a tutorial on graphics programing with WolframLang.
We cover the following most important things:
- Graphics Primitives
- Displaying Graphics Primitives
- Graphics Directives
- Extracting Graphics Primitives from builtin functions
Graphics Primitive
- Graphics Primitive is a expression that represents a graphical element, such as line, circle, polygon, text, sphere, tube.
- You use graphics primitive to create graphics, programatically.
- All Plot functions or visualization functions that show graphics, are internally creating a list of graphics primitives, and return it as output.
some graphics primitives are 2D (such as Circle, Disk, Rectangle), some are 3D (e.g. Sphere, Cuboid, Tube), some both (e.g. Point, Line, Polygon).
Example of graphics primitives:
Point[{45,41}]
(2D)Point[{44,90,48}]
(3D)Line[{{331,754}, {557,550}, {888,272}}]
(2D)Line[{{92,402,927}, {183,643,101}, {229,39,925}}]
(3D)
Polygon[args]
(2D or 3D)Text[args]
(2D)Disk[args]
(2D)Circle[args]
(2D)Rectangle[args]
(2D)Sphere[args]
(3D)Cuboid[args]
(3D)Tube[args]
(3D)
Complete list at SymbolicGraphicsLanguage
Graphics primitives are symbolic. By themselves they do nothing.
Line[{{0, 0}, {2, 1}}]
Graphics primitive usually are bundled together in a list. e.g.
{Line[arg], Line[arg], Polygon[arg], etc}
Displaying Graphics Primitives
To display graphics primitives,
put them inside the functions
Graphics
or
Graphics3D
.
Example
Graphics Directives
Graphics Directives are things like color, thickness. They control the style and rendering of the graphics primitives.
Here are commonly used graphics directives:
Red
Green
Blue
Hue
GrayLevel
PointSize
Thickness
EdgeForm
FaceForm
Graphics directives are used in a list, where they precede the graphics primitives. For example:
How to Extract Graphics from Builtin Plot Functions
Typically, all builtin functions that display graphics return a list such that the first element is a list of graphics primitives (and directives).
so, to exctract them, you can just use First on the result.
(* extract the graphic primitives from a builtin visualization function *) gr = Plot[ Sin[x], {x,0,5} ]; Length[gr] === 2 Head[gr] === Graphics (* list of graphics primitives and directives. usually big. *) First[gr]
Advanced: Discretize Graphics
DiscretizeGraphics and DiscretizeRegion return a MeshRegion. These are critical, when you want to do a transformation on graphics, so that it is applied all points on edge or faces, instead of just on vertexes.