How to make pictures using postscript program (part 4) ----------------------------------------------------------------------------- In Example Program 6, we kept track of how much we moved in each iteration of the for loop, and the moved back by that accumulated amount. A simpler solution is just to save the current coordinate system before the for loop, and restore it afterwards. The coordinate system is part of the graphics state, and the command gsave saves the graphics state. grestore restores the graphics state that was saved before. So gsave and grestore always have to occur in matching pairs, like brackets. --------------Example Program 8---------------------------------------------- %!PS-Adobe-2.0 %%Title: A grid of crosses %%BoundingBox: 0.0 0.0 550.0 750.0 25 25 translate /cross {newpath -5 0 moveto 5 0 lineto stroke newpath 0 -5 moveto 0 5 lineto stroke } def 1 1 35 {gsave 1 1 25 { cross 20 0 translate } for grestore 0 20 translate } for showpage ----------------------------------------------------------------------------- Up to now we just used the path we constructed to draw a line along this path. There are many other things we can do with it. First, the path can be in various colors, thicknesses and other drawing styles. 2.5 setlinewidth sets the thickness of the line to 2.5: a thicker path. The default value is 1. You can also draw thin paths, for example 0.01; at some point they get to thin to be displayed well. 0.5 setgray sets the greyscale value used for current drawing; default is 0 (black), can be any number up to 1 (white). 0.1 0.7 0.1 setrgbcolor makes the current drawing in color instead of b/w. The first value is the red, then green, the blue value. Each of these numbers is between 0 and 1. There are a number of further drawing parameters, like dashed lines, or the way several line segments are joined together (rounded or angular). ----------------------------------------------------------------------------- The command closepath connects the current point back to the beginning of the path, to make a closed polygon. Thus newpath 0 0 moveto 9 0 lineto 9 9 lineto 0 9 lineto closepath creates a closed square path. We can draw that path, but since it is closed, it is also meaningful to fill that outline. That is done with the command fill. --------------Example Program 9---------------------------------------------- %!PS-Adobe-2.0 %%Title: Some colored squares %%BoundingBox: 0.0 0.0 550.0 550.0 25 25 translate 0 0 1 setrgbcolor newpath 0 0 moveto 400 0 lineto 400 400 lineto 0 400 lineto closepath fill 25 25 translate 0 1 0 setrgbcolor newpath 0 0 moveto 300 0 lineto 300 300 lineto 0 300 lineto closepath fill 25 25 translate 1 0 0 setrgbcolor newpath 0 0 moveto 200 0 lineto 200 200 lineto 0 200 lineto closepath fill 25 25 translate 1 1 0 setrgbcolor newpath 0 0 moveto 100 0 lineto 100 100 lineto 0 100 lineto closepath fill showpage -----------------------------------------------------------------------------