Drawing with postscript I, 30th of November 2012.

a circle pressed into a square

I left behind me two weeks of work on Jožef Stefan Institute in Ljubljana. It seems to me that I accomplished some interesting feats in a successful collaboration with Primož. All we have to do now is to publish it all in some decent journal and finish the story which started two years ago on Christmas Biophysics Workshop in Ptuj.

But, I won't in this post write about the physics I worked on with Primož. I will write about images we, among other things, studied, and which are the output of the code I wrote. My code, namely, produces a postscript image as one of its outputs. One such image, a result of my code, is shown above.

Postscript is a language to "program" the image, i.e. to "describe" the page - that is why it is called the "page description language". Although postscript was conceived as a language for printers, one can read a well written postscript code and get an, at least approximate, idea of the image it produces i.e. codes for.

This post should serve as an elementary introduction to postscript, after which you should be able to make images shown above and below. One needs really a very limited knowledge of postscript in order to make these images, but it should suffice for some quite complex visual representations. In future, I will probably write a couple more of posts on postscript in which I will explain its more advanced possibilities.

columnar compression of a disk

A most elementary knowledge of postscript is already enough to make images which would be impossible to make "by hand" (see e.g. the image above). For a start, each postscript code must have a heading. Here is my heading (the first line; it says that the code which follows belong to level 2 postscript) and the two commands which define how the lines end (setlinecap) and the way they join (setlinejoin):

%!PS-Adobe-2.0
gsave
1 setlinejoin
1 setlinecap


Instead of the option 1 I chose, one can also choose other options, and how the line ends and line joins look in that case can be seen in >> a quick introduction to postscript written by Paul Bourke, which I warmly recommend. His text is 22 years old, yet it is completely usable, which hints that postscript is a technology that is here to stay for some time.

After the heading, one needs to start drawing. The lines, polygons, circles and similar, are defined in the coordinate system of the page whose x-axis points to the right and the y-axis to the top of the page, i.e. the origin of the page is in its lower left corner. A unit length (1) of such a system is 1/72 of an inch. A4 paper has 595 x 842 area in these units. Postscript allows for non-integer lengths, i.e. coordinates.

A new "path", i.e. a new graphics element, starts with the command:

newpath

Before drawing the first line, we define its color (setrgbcolor), i.e., in general, the color we shall use from now on, for any object that we will draw:

0.035 0.000 0.000 setrgbcolor

The color is defined as an RGB triple, with white color given as RGB=(1,1,1). The color defined above is thus a very dark red. The definition of color is valid all until the next setrgbcolor statement / command where one can setup some other color.

Now we draw the first line. It is good to imagine that with our code we define a cursor which is moving on the paper. First we drag it (moveto) to the first coordinate of the line:

107.948 340.212 moveto

And then we tell it to draw the line (lineto) from that point (107.948, 340.212) to the next one (116.177, 353.291):

116.177 353.291 lineto

To really draw the line, we hit the command:

stroke

So, that's how it goes. One just needs to draw a lot of lines. Here is how the whole code looks like:

%!PS-Adobe-2.0 gsave
1 setlinejoin
1 setlinecap
newpath
0.035 0.000 0.000 setrgbcolor
107.948 340.212 moveto
116.177 353.291 lineto
stroke
0.019 0.000 0.000 setrgbcolor
107.948 340.212 moveto
115.358 326.578 lineto
stroke
0.035 0.000 0.000 setrgbcolor
107.948 340.212 moveto
123.481 339.605 lineto stroke

...
grestore
showpage


With the two last lines, the postscript file is closed, and the page we defined is shown (showpage). And that's it.

The image below shows another example, and you can see its postscript code by opening >> THIS file in some text editor. In order to see the postscript files, you need some viewer. I mostly use >> Ghostview (Ghostscript).

triangular network

To end this post, I give you another example in which I used postscript in order to visualize a virus i.e. fullerene (below).

Note that in the cases shown above and below I had to write a small routine in my code which transformed three-dimensional coordinates of the shape to two-dimensional postscript coordinates. I did it using simplest perspective projection which I intend to describe in some future post.

hexagonal network

To all those who were inspired by the text above and want to learn more about postscript, I can recommend a famous "blue book". You can learn almost everything there is to learn about postscript from that book. >> Here is a link to The Blue Book.

Here is a link to >> the second part of the introduction to postscript. And >> HERE is the third part of the introduction to postscript.

<< Haiku about loneliness Sake haiku >>

Last updated on 30th of November 2012.