June 1998.

That’s when I wrote the code I’m going to talk about today.

When I became interested in computer graphics, a windows environment you’re used to by now just didn’t exist. For example, the computer I used (called a Zenith H19) had a screen 80 x 25 *characaters.* Yes, *characters.* Rather like a larger version of those still unimaginable TI calculator screens (couldn’t resist).

There was no graphical user interface (GUI), mouse, touch-screen. If you wanted to draw a picture, you had to write a computer program to do it.

And if you wanted to see what it looked like, well, you had to print it out.

Yep. That was the *only* way to see if your code was correct. It made debugging very challenging. But that’s all there was. So I learned how to write PostScript code.

There are two features of PostScript I want to discuss today. First, the syntax is written in postfix notation, like most HP calculators. If you wanted to add 2 and 3 on a typical calculator, you’d type in “2 + 3 =.” The result would be 5. This is called *infix* notation, where the operator is written in between the arguments.

In PostScript, though, you’d write “2 3 add.” In other words, the arguments come *first.* Then they’re added together — the operator comes last, which is referred to as *postfix* notation. So if you wanted to write

(2 + 3 ) x (10 – 4)

in PostScript, you’d write

2 3 add 10 4 sub mul.

Notice that the multiplication is done last.

When programming in Python, most functions are described using *prefix* notation. In other words, you give the function name first, and then the arguments to the function. For example, randint(1,10) would output a random number between 1 and 10, inclusive. If we had to write the arithmetic expression above in prefix notation, it would look like

times(add(2,3), subtract(10,4)).

You probably encounter these ideas on a daily basis. For example, if you want to delete a bunch of files, you first select them, and then do something like click “Move to trash” from a dropdown menu. But if you’re writing an email, you usually click on the attach icon first, and then select the files you’d like to attach. Postfix and prefix.

Of course this is just an introduction to using the different types of notation. In general, you’d transform the above arithmetic expression into what it called an *expression tree,* like this:

Then prefix notation may be derived from a *preorder traversal* of the expression tree, infix notation from an *inorder traversal* of the tree, and finally, postfix notation comes from a *postorder traversal* of the tree. Feel free to ask the internet all about binary trees and tree traversals. Trees are very important data structures in computer science.

The second feature of PostScript I’d like to discuss today is that it is an example of a *stack-based language.* For example, if you typed

4 5 3 7 8

in a PostScript program, the number 4 would be pushed onto the *stack — *that is, the working memory of the program — and then 5, then 3, 7, and finally 8. (For convenience, we’ll always think of the *bottom* of the stack as being on the left, and the *top* of the stack as being on the right. Easier to write and read that way.)

If you want to follow along, do the following: write 4 on an index card or small piece of paper, then write 5 on another and put it on top of the 4, and so on, until you put the 8 on top of the stack. You really *do* have a stack of numbers.

Now when you type the “add” function, PostScript thinks, “OK, time to add! What are the top two numbers on the stack? Oh, 8 and 7! I’ll add these and put the sum back on the stack for you!”

So you type

4 5 3 7 8 add,

and PostScript performs the appropriate action and the stack now becomes

4 5 3 15.

If you now want to divide (div in PostScript), the interpreter looks at the top number on the stack and divides it by the second number on the stack, and puts that result on the stack. So

4 5 3 7 8 add div

would create the stack

4 5 5.

You should be able to figure out that

4 5 3 7 8 add div mul sub

would just leave the single number 21 on the stack.

Graphics are drawn this way, too. For example, we would create a square with the commands

0 0 moveto 0 1 lineto 1 1 lineto 1 0 lineto 0 0 lineto closepath stroke.

Again, the arguments come first. We move to the point (0,0), and then draw a line segment to (0,1), and so forth. Closepath “ties” the beginning and end of our path together, and the command “stroke” actually draws the square on the page.

Commands like

1 0.5 0 setrgbcolor

set the current color to orange, and

5 setlinewidth

sets the width of lines to be drawn. You can change any aspect of any drawn image — the only limit is your imagination!

Of course this is only the most basic introduction to the PostScript programming language. But if you’re interested, there’s good news. First, it shouldn’t be hard to download a PostScript interpreted for your computer. I use MacGhostView — installing will also require you to install XQuartz, too, but not to worry.

Second, there are some great resources online. Back when I was learning PostScript, there were three very helpful books — the Blue Book, the Red Book, and the Green Book. Yep, that’s what there were called. The Blue Book is the place you should start — it includes a tutoral on all the basics. The Red Book is the Reference Manual — handy in that it lists PostScript commands, their syntax, and examples of how to use them. The Green Book is for the serious programmer, and discusses at a high level how the PostScript language is organized.

Finally, here is the code I wrote all those years ago! (Click here: Sierpinski.) I did modify the original code so it produces a Sierpinski triangle. It will be a little daunting to figure out if you’re just starting to learn PostScript, but I include it as it motivated me to talk about PostScript today.

And a final few words about motivation. Try to learn programming languages which expose you to different ways of thinking. This develops flexibility of mind for coding in general. Whenever I learn a new language, I’m always thinking things like, “Oh, that’s the same as stacks in PostScript,” or “Yeah, like maps in LISP.”

As you learn more, your programming toolbox gets bigger, and you find that the time it takes from conceiving of an idea to successfully implementing it becomes shorter and shorter. That’s when it starts *really* getting fun….

## One thought on “Fractals VIII: PostScript Programming”