Eric Weeks - personal pages - graphics techniques

Simple PostScript - Part II

circles, filling shapes
[home] [research]
[software] [pics]
[misc] [about me]

weeks@physics.emory.edu

Click here to see the introduction to PostScript page (Part I).


Circles and Arcs

To draw a circle or an arc, use the "arc" command. You must specify the center, radius, and starting and ending angles (in degrees). Also, if you are drawing a circle, you should end with "closepath" in order to join the two ends of the arc. This becomes more important when you're filling the circles but it's a good habit to get into.

So, a typical circle command looks like:

4 5 3 0 360 arc closepath stroke

which puts a circle at the coordinates (4,5) with a radius of 3. As usual, nothing is actually drawn until you say "stroke".

As with drawing straight lines, the command "setgray" changes the color of the line drawn and the command "setlinewidth" will affect the width of the line. The actual code that generated the above picture was:

0 setgray 0.1 setlinewidth 4 5 3 0 360 arc closepath stroke


To make arcs, just change the starting and ending angles:

4 5 3 0 360 arc closepath stroke 5 4 2 0 120 arc closepath stroke


There is also a command "arcn" which draws arcs in the opposite direction; for example, the above arc commands could have been written:

4 5 3 360 0 arcn closepath stroke 5 4 2 120 0 arcn closepath stroke

This is only useful when you want to fill the circles.

Filling shapes

If you have a shape (circle, arbitrary polygon) which you have drawn and closed with "closepath" you can fill the shape. Thus:

4 5 3 0 360 arc closepath 0.7 setgray fill

One side affect of the fill command is that it forgets the current path, as if you had used the "newpath" command. This is the same behavior as "stroke". If you want to fill a shape and also show it's outline, you need to save the current path before you fill it, and then restore the path to draw it (show the outline). You do this with the commands "gsave" and "grestore". They essentially save the entire current PostScript state, and then restore it. Your code will then look like:

0 setgray 0.1 setlinewidth 4 5 3 0 360 arc closepath gsave 0.7 setgray fill grestore stroke

Note that the final stroke command will be black; the color is temporarily changed to light gray, but then "grestore" restores the old color.

Advanced Filling

Suppose you want to fill a shape with a hole in it (like a donut). You have to be careful about the direction you draw the lines in. The outside line would be drawn in a counter-clockwise direction, and the inside line is drawn in a clockwise direction. Generally I don't remember this rule; I just try it both ways until it works. (In order to draw circles in the clockwise direction, use the "arcn" command discussed above).

4 5 3 0 360 arc closepath 4 5 2 360 0 arcn closepath 0.7 setgray fill

Using everything

You might want to take a look at the page describing how to use color in PostScript; it's very easy.

%!PS-Adobe-1.0 matrix currentmatrix /originmat exch def /umatrix {originmat matrix concatmatrix setmatrix} def [28.3465 0 0 28.3465 10.5 100.0] umatrix 0 setgray 0.2 setlinewidth % head 4 5 3 0 360 arc closepath stroke % mouth 4 5 2 210 330 arc closepath gsave % this is red: 1 0 0 setrgbcolor fill grestore stroke % eyes 3 6 0.8 0 360 arc closepath gsave % this is blue: 0 0 1 setrgbcolor fill grestore stroke 5 6 0.8 0 360 arc closepath gsave % this is blue: 0 0 1 setrgbcolor fill grestore stroke showpage

Click here to get a copy of the PostScript code

Color PostScript commands

Links...


Current address:
Eric R. Weeks
weeks(circle a)physics.emory.edu
Department of Physics
Emory University
Atlanta, GA 30322-2430