Postscript Fractals…

This is a silly fun thing to do on a rainy day. Postscript is a language that might not be in fashion anymore. Perhaps it might be because it is a bit backwards since it is a stack-based language and was designed to be used by machines (that is printers) rather than people. It is quite rich in vector graphics related functionality. Also it is great for dealing with recursion (such as saving and restoring graphics state variables on a separate stack). Another reason for using postscript is that I want vector graphics, that is having lines/shapes that stay clean and crisp under any level of zooming.

An iterated function system (or IFS) fractal are ‘fun’ mathematical objects since they merge math, art, and science together. There is quite a bit of information about fractals out there… like here, here, and here… so I am not going to get deep in the theory. Anyways, an IFS fractal is the union of several transformed copies of itself. The transformations are a set of functions that are (usually) contractive mappings, that is they reduce the size of shapes; also the functions alter the positions and may even rotate the coordinate system. The IFS is applied on itself any number of times, but often only a small of iterations are needed since the IFS will converge rather quickly.

Dealing with PS files

There are a few different techniques to deal with postscript files. These include:

  • Use a program like ghostscript
  • Convert it to a pdf using ps2pdf, which is free and available (usually out of the box) on Linux and Mac systems
  • On a Mac, double click to let preview convert it to a PDF

Overall Algorithm

The overall algorithm (written in a hybrid postscript pseudo code) is listed below (note that saving the graphics state is a postscript thing). Also to keep things simple, ‘pop’s, ‘dup’s, and utility functions were removed.

% Function to make a shape...
/Shape {
    % newpath
    %    Starting from (0, 0) create a path 
    % closepath

    % either fill or stroke the path (or both)
} def

% Function to make the IFS fractal
/DoIFS {
    % check to see if the value on the stack is greater than 0
    % if it is...
    %    subtract one from the top of stack value
    %
    %    for each 'element' of the IFS:
    %        save the graphics state
    %            set a new origin and scale the coordinate system
    %            call DoIFS
    %        restore the graphics state 
    % else...
    %    draw the shape
} def

% Finally make the fractal
% put some number on the stack... and call DoIFS

showpage

Note that for some (such as those in crazy minimal code contests) this code can be considered rather bulky. I would rather have a bit of extra code and whitespace so I can maintain this in the future.

Results

For now, I have made two of the classic IFS fractals, namely the Sierpinski triangle (or gasket) and the Sierpinski square (or carpet).

Code

Finally, the postscript code can be found here.

 

No Comments

Add your comment