Spirally thingNot much point to this post, but I played with PostScript a bit today. It brings me back to what interested me in computers in the first place: pointless graphics. I actually have a memory of being 4 or 5 years old and asking my dad for the millionth time the BASIC instruction to get our TRS-80 into graphics mode, and feeling bad about having to ask him for the millionth time. There’s just something about pushing pixels around that’s alluring for me.

PostScript is just a tonne of fun. If you have a PostScript interpreter (Preview under OS X, Ghostview under everything else), it’s easy to get it up and running and playing with stupid things.

The following is the PostScript code that was used to generate the picture you see to your right. Copy and paste this into a file called foo.ps or some such and view it. It looks much prettier than in image form.

%!PS
300 400 translate       % get somewhere in the middle
/point0 [0 0] def
/point1 [0 20] def
/point2 [-20 20] def
/point3 [-20 0] def
/nextline {
        % here's how the nextline function works. we have a box like so:
        %     point2 .____________. point1
        %            |            |
        %            |            |
        %            |            |
        %            |            |
        %     point3 .____________. point0
        % I've drawn it nicely horizontally/vertically, but really these will
        % be at weird angles to each other (except the first iteration). we draw
        % a line from point1 to point0, except we go PAST point0 by 5 units
        % (i.e, just a wee bit). that new point becomes the new point1, point3
        % becomes the new point0, the old point2 becomes the new point3, and
        % so on. keep doing that and you get a pretty spirally box thing.
        /xdiff point0 0 get point1 0 get sub def
        /ydiff point0 1 get point1 1 get sub def
        /alpha xdiff ydiff atan def
        % define the new target point
        /ntp [  % the new x co-ordinate
                point0 0 get 5 alpha sin mul add
                % the new y co-ordinate
                point0 1 get 5 alpha cos mul add] def

        % draw the line
        ntp 0 get ntp 1 get lineto

        % cycle back the points
        /point0 point3 def
        /point3 point2 def
        /point2 point1 def
        /point1 ntp def
} def
point1 0 get point1 1 get moveto
600 {nextline} repeat
stroke
showpage

That’s it. For those not familiar with PostScript, it’s a stack based language. When you’re calling a function, you list the operands first, and then the operation. That operation will pop the top elements off the stack. E.g., 5 5 lineto will draw a line from the current point to (5, 5). 3 7 add 3 lineto will draw a line to the point (10, 3)—first it executes 3 7 add, pushes a 10 on the stack, and then you’re left with 10 3 lineto on the stack. /variablename expr def is a way of defining variables—the def operator adds a symbol to the dictionary. Curly braces introduce functions. Square brackets introduce arrays, and the get operation returns an element from an array.

What’s cool is that if you send this to a PostScript printer—most laser printers that have been manufactured in the past 20 years—the printer itself will execute the program and draw the lines itself.

Anyway, now I have to drag myself away from PostScript to do some real work. It’s just such a beautiful language, perfectly designed for mindless drawing. Much better than the BASIC I grew up on, or, god forbid, the Java we teach first year students to do their drawings in.

Advertisements