Eric Weeks
- personal pages - graphics
techniquesSimple PostScript - Part IIcircles, filling shapes |
weeks@physics.emory.edu |
Click here to see the introduction to PostScript page (Part I).
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:
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:
To make arcs, just change the starting and ending angles:
There is also a command "arcn" which draws arcs in the opposite direction; for example, the above arc commands could have been written:
This is only useful when you want to fill the circles.
If you have a shape (circle, arbitrary polygon) which you have drawn and closed with "closepath" you can fill the shape. Thus:
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:
Note that the final stroke command will be black; the color is temporarily changed to light gray, but then "grestore" restores the old color.
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).
You might want to take a look at the page describing how to use color in PostScript; it's very easy.
Click here to get a copy of the PostScript code