Drawing primitives
It is about time we talk about drawing with PostScript!
The above doodle is created with some of the drawing primitives that PostScript provides. To wet our appetite we will list the entire program below
newpath
0 0 moveto
100 100 lineto
100 150 50 -90 180 arc
50 50 0 100 0 0 curveto
closepath
stroke
showpage
Current Path
The central concept in PostScript programs is the path. The drawing primitives construct paths. You might remember from the "Hello, World!"-program that your first construct a path and then stroke it. Later we will learn that paths can also be filled.
The path that is being constructed is called the current path. The drawing operators change the current path in certain ways.
newpath
The newpath
operator clears the current path and starts an empty path, ready to be altered to your hearts content.
moveto
The moveto
operator changes the current point. The current point is an important building block for drawing primitives. Usually drawing primitives change the current point to be the current point to the last control point.
The moveto
operator requires two operands to be present on the stack. Those are the x
-coordinate and the y
-coordinate of the current point to be.
No marks will be left on the paper with a moveto
operator.
lineto
If you do want to leave a straight line segment as a mark on the paper, use the lineto
operator.
The lineto
operator requires four operands. The x
- and y
-coordinates of the start point and the x
- and y
-coordinates of the end point. The current point will be set to the end point.
arc
The arc
operator and variants create a circular arc. It needs 5 operands. The x
- and y
-coordinate of the center of the arc, the radius of the arc and the two angles in degrees.
arc
creates a counter-clockwise arc from the first angle to the second angle. The arcn
variant produces a clockwise arc from the first angle to the second angle.
Furthermore, a line segment is added from the current point to the first point, the point at the first angle, of the circular arc.
The following snippets show the various options
0 0 moveto
100 100 50 0 270 arc
0 0 moveto
100 100 50 270 0 arc
0 0 moveto
100 100 50 0 270 arcn
0 0 moveto
100 100 50 270 0 arcn
Their result collated in a single image:
curveto
The curveto operator extends the current path with a cubic bezier curve.
Bezier curves are fascinating and well-behaved drawing tool. We will not say a lot about it now, but feel free to pick the brain of the workshop leaders.
closepath
The close path operator will draw a line segment from the current point to the point designated by the last moveto operator.
There is a subtle difference, that will be noticeable under close scrutiny, between drawing the segment yourself and using closepath.
Relative drawing primitive
The above drawing primitives all accept operands. When the operands are coordinates they are global. For example if you want to draw a square with the lower left corner at 10 10 and with side length of 30 one could use the following program
10 10 moveto
40 10 lineto
40 40 lineto
10 40 lineto
closepath
The problem is that we need to calculate the endpoints. We can let PostScript do the heavy lifting, but there is an alternative. Take a look at the following snippet
10 10 moveto
30 0 rlineto
0 30 rlineto
30 neg 0 rlineto
closepath
The big difference is the use of rlineto
. This will draw a line segment with a relative to the current point.
Almost all the mentioned drawing primitives have such a relative cousin.
Reference
There are more drawing operators, all with a specific need. Listing them all isn't very exciting. Instead I will point you to the reference.
Exercises
- Match the
arc
snippets to the drawing. - There is no relative
arc
operator. Provide one yourself. Hint: take a look at thecurrentpoint
operator.