Unnatural postscript hacks

Tim Vaughan and I wrote the following postscript programs, which revolve around doing unusual amounts of processing on printers. I've got to give Tim the credit for introducing me to this fantastic idea (cheers Tim!).

I don't think it's generally appreciated, but Postscript is a complete programming language in it's own right, letting you do pretty much any piece of computation on a postscript printer. As I understand it, the language is very similar to Forth - both are heavily stack-oriented. Think of programming an old HP calculator and you'll get the idea. For the record, none of the programs below send any raster graphics to the printer :)

The Programs

A simple raytracer

Ever wanted to benchmark your postscript printer? Well here's a perfect opportunity to do just that! psRay is (mostly) the product of a happy weekend's hacking and shows that you really can write pretty much arbitrary programs in postscript. PsRay basically implements a ray-tracer in postscript. I have to admit that the inspiration for this partly came from the docs for the excellent free raytracer povray - the povray team were perverse enough to write a raytracer in their scene description language. It's really a pretty basic proof-of-concept (or perhaps proof-of-craziness?) and there's plenty of things which could be added like colours and other geometry types, but I'm inclined to keep it simple and clean.

psRay.ps

Notes:

  1. Expect older printers to get bogged down processing this one - accidentally sending this program to the wrong (older) printer in the physics department once resulted in a five hour wait (!)
  2. You may also find that the printer complains, or just seems to forget the job altogether. I suspect this is a problem with postscript noncompliant hardware - the effect is inconsistent between printers - but I'm happy to learn about any bugs.

I was recently disappointed to learn that I'm not the first to implement this piece of crazyness... Apparently someone got there more than 10 years before me - what is ever new on the net? I've decided to take comfort from the fact that they just translated theirs, while mine was written from scratch without looking up any references :) Of course this begs the interesting question: how many independent implementations of postscript raytracers are out there? Let me know if you find any others!

After putting the above up, Evan Danaher has kindly sent me a very nice example of a postscript raytracer. The example is both incredibly obfusificated, and incredibly much faster than my version above. It was apparently written by a guy named Takashi Hayakawa, which may be found here. Evan also modified it into a colour version which is also quite cool.


1D cellular automata

Everyone loves cellular automata, so here's Tim's great implementation of a one dimensional version. The best thing about this program is that it represents an open-ended time evolution. This means that a simple change to the code yields an infinite number of pages of output. (OK, well at least in principle. In practise I guess it's until the paper runs out, the sysadmin discovers your evil ways, or you feel too guilty about the number of dead trees you're wasting.)

1dca.ps

An affine IFS fractal generator

A implementation of Barnsley's famous iterated function systems; these are one of my favourite ways of generating fractals because of their extreme versatility. IFS were popularised by Barnsley in his fantastic book "Fractals Everywhere": Given a picture (= set in R2), the collage theorem allows us to find a fractal map with an attractor which looks very similar to the original set. Fascinating stuff. Of course there's plenty of pretty pictures which you can create as well by randomly choosing a set of affine maps.

affineIfs.ps

Bubbly Mandelbrot - Tim's original!

Here's the image which used to reside on Tim's wall and started this whole thing off... Points in the Mandelbrot set are rendered with circles.

bubbles.ps

The Julia set

Well, if the Mandelbrot set is hanging around, we'd better have a Julia set as well. This program uses inverse Julia iteration to converge to the Julia set, and plots the points as it goes. For those who are interested there's a deterministic recursive algorithm or a Monte Carlo version though both produce visually the same output. This program brings up some interesting artifacts due to the limited floating point precision specified in the postscript spec. (If I recall rightly we only have 6 or so digits of precision to play with.)

julia.ps

The Henon attractor

Here's Tim's version of the Henon attractor. I'm afraid I don't know much about this one, but Wikipedia tells me it's a simplified model of a Poincare section of the Lorenz attractor. More interesting, it depends on some parameters which may be adjusted to change the set all the way from the disconnected Cantor set at one extreme to a smooth curve at another. Wow.

henon.ps

Instructions for M$ users...

For those of you who are unlucky enough to be burdened with the use of M$ windo$e, you have my pity. Oh, and if you don't own a postscript printer, you should download and install gsview and ghostscript if you want to look at the pictures above. Then just open up the files in gsview... Of course, the enlightened among us will just use gv for a preview ;)