Postscript and Geometry


What is Poscript?
Basic operations
The coordinate transformation
Homogeneous coordinates
What can Postscript do?
How does Postscript render graphical information?
But why would you want to write your own code?
Let's get jiggity
Resources
ADDANCHOR whazzit FIRSTANCHOR What is Postscript?

A programming language whose instructions, when read by a Postscript interpreter, produce graphical output.

Typically, Postscript programs are written by other programs like Adobe Illustrator or dvips. These are never meant to be read by humans.

This talk, however, is about Postscript programs written by humans.

Postscript has two beautiful features: (1) everything (well, almost) is drawn to within the accuracy of the output device and (2) everything is easily scaled. ADDANCHOR basic ANCHOR Basic operations

Computations are performed on a "stack"

 3 4 -1.5 add mul
 4 -1.5 add mul
 -1.5 add mul
 add mul
 mul
ANCHOR Graphical operations

Postscript's coordinate system

ANCHOR Drawing lines

100 100 moveto
450 600 lineto
stroke
ANCHOR Drawing circles
300 300 100 0 360 arc 
stroke
ANCHOR Filling circles
300 300 100 0 360 arc 
gsave
  0.8 setgray
  fill
grestore
10 setlinewidth
stroke
ANCHOR Drawing Bezier curves
200 200 moveto
100 500 
500 300 
400 550 
curveto 
stroke
ANCHOR Displaying text
100 100 moveto
(g) show
ADDANCHOR transform ANCHOR The coordinate transformation

The initial coordinate transformation

User space Device space
ANCHOR Translations
200 300 translate
User space Device space
ANCHOR Rotations

200 300 translate 
45 rotate 
User space Device space
ANCHOR Scaling

200 300 translate 
0.8 3 scale
User space Device space
ADDANCHOR homogeneous ANCHOR Homogeneous coordinates

Everything produced is passed through an affine coordinate transformation from user space into device space. This transformation has the form

or in the language of linear transformations

ANCHOR

This is typically expressed in homogeneous coordinates

In this formalism,

ANCHOR General transformations

200 300 translate
[1 0 0.5 1 0 0] concat
User space Device space
ANCHOR General transformations

200 300 translate
[0 1 1 0 0 0] concat
User space Device space
ANCHOR What do you mean everything?

Well, everything: lines, curves and text are all described in user space, then transformed into device space before rendered. To understand how this works, let's take a look under the hood. ADDANCHOR whatcando ANCHOR What can Postscript do?

Apparently, not much: ultimately everything in Postscript is described in terms of lines and Bezier curves--even circular arcs:

ANCHOR and text

There is, however, a great benefit to be gained. ADDANCHOR render ANCHOR How does Postscript render graphical information?

Postscript draws a line between two points

ANCHOR

recursively by drawing the lines between the endpoints and midpoint

ANCHOR

and again and again

ANCHOR

until finally

ANCHOR Bezier curves

The four points define the curve

where

ANCHOR These can be built recursively as well.

ANCHOR The curve splits into two other Bezier curves whose control points are easily obtained.

ANCHOR Just keep dividing the smaller pieces

ANCHOR until

ANCHOR So what? This means that to transform a line or a curve, all we need to do is transform the control points. That's easy.

ADDANCHOR program ANCHOR But why would you want to write your own code?

The Economics of Computing
After making a few widgets, the marginal cost of making more widgets is almost zero.
ANCHOR

Postscript is a full fledged programming language with loops and other good things.

ANCHOR To build this:

1. Draw big triangle
2. Scale by 1/2 and draw smaller triangle
3. If not at the lowest level, repeat three times:
  • Save the coordinate transformation
  • Translate by 0 -s
  • Draw next triangle
  • Go to step 2
  • Restore coordinate transformation
  • Rotate by 120
  • ANCHOR From geometry

    The Elements, Book I, Proposition 1

    ANCHOR

    The Nine Point Circle

    ANCHOR From calculus

    ANCHOR Introducting Wooody, the loveable star of Toy Tale

    Transform Wooody ANCHOR Wooody emotes

    ADDANCHOR jiggity ANCHOR Three dimensional drawing

    ANCHOR Orthogonal projection

    ANCHOR Perspective projection

    ANCHOR

    ANCHOR More homogeneous coordinates


    and then

    ANCHOR

    Let's see: we need

    so we'll take

    We can accomplish this by

    ANCHOR Shadows are obtained in the same way

    ADDANCHOR resources LASTANCHOR Resources

    Adobe, creators of Postscript

    Ghostscript, a free Postscript interpreter and viewer

    Guide to Postscript Programming

    Bill Casselman's Geometry and Postscript, a text book used to teach PostScript within a Geometry course.

    Bill Casselman's Mathematical Graphics resources page.

    Peter Kleiweg's Postscript page

    P.S. All figures have been drawn in Postscript.