WolframLang: Graphics Primitives

By Xah Lee. Date: . Last updated: .

Here's a tutorial on graphics programing with WolframLang.

We cover the following most important things:

  1. Graphics Primitives
  2. Displaying Graphics Primitives
  3. Graphics Directives
  4. Extracting Graphics Primitives from builtin functions

Graphics Primitive

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:

Complete list at SymbolicGraphicsLanguage

Graphics primitives are symbolic. By themselves they do nothing.

Line[{{0, 0}, {2, 1}}]
WolframLang Line 2022-02-09 4kgq

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.


WolframLang Graphics 2022-02-09 f3D4
Graphics[Line[{{0, 0}, {2, 1}}]]
Cuboid 2022-02-09 5g87
Graphics3D[Cuboid[{0, 0, 0}, {1, 2, 1}], Axes -> True]
WolframLang cubes 2022-02-09 mJgr
Graphics3D[Table[Cuboid[{x, x, x}, {x, x, x} + 1], {x, 0, 5}], Axes -> True]

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:

Graphics directives are used in a list, where they precede the graphics primitives. For example:

WolframLang graphics directive 2024-02-24
Graphics[{Red, Circle[ ]} , Axes -> True ]

Graphics[{Red, Circle[ ], Line[{{0,0},{1,1}}] }, Axes -> True ]
WolframLang graphics directive 2024-02-24 FB5n
Graphics[{Red, Line[{{0,0},{1,1}}], Blue, Thick, Line[{{0,0},{1,2}}] }, Axes -> True ]

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. *)

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.

WolframLang: Graphics Programing