How to make pictures using postscript programs (part 6) ----------------------------------------------------------------------------- Instead of keeping track of the stack content, we can put a number into a variable, so give it a name. The mechanism is the same command def we used to define pieces of code. def takes a name and an object, and causes the name in future to be replaced by the object. The object can be a number, a command or piece of code, or indeed anything. /radius 20 def defines radius to be 20, any further occurence of radius will be replaced by 20, until radius is defined to be something else. ----------------------------------------------------------------------------- To put a name on the stack, this name is prefixes by a slash / otherwise the postscript interpreter will attempt to evaluate the name. So if we write radius 30 def radius will be replaced by its current value, perhaps 20, and def does find two numbers on top of the stack, not a name and a number. The name must be before the number. If the number is already there, you put the name after it, and then exchange those top two items before the command def. So a typical assignment of the top number on the stack to the name radius looks like /radius exch def ---------------------Example Program 12-------------------------------------- %!PS-Adobe-2.0 %%BoundingBox: 0 0 500 500 10 10 250 {/radius exch def %sets radius to 10 20 30 ... 250 250 250 radius 0 360 arc stroke } for showpage ----------------------------------------------------------------------------- This creates a sequence of circles around point 250 250 of radius 10, 20,..250 ---------------------Example Program 13-------------------------------------- %!PS-Adobe-2.0 %%BoundingBox: 0 0 550 550 20 20 translate newpath 0 0 moveto 500 0 lineto stroke %horizontal axis newpath 0 0 moveto 0 500 lineto stroke %vertical axis newpath 0 0 moveto %starts curve in point 0 0 0 10 500 {/x exch def %sets x to 0 10 20 ... 500 /y x x mul 500 div def %sets y to x*x/500 x y lineto %makes x y next point along the path } for stroke % draws line along the path showpage ------------------------------------------------------------------------------ This draws a parabola y = (1/500)x^2 from x=0...500 with two axis ---------------------Example Program 14-------------------------------------- %!PS-Adobe-2.0 %%BoundingBox: 0 0 400 410 0 10 400 {/a exch def newpath 0 a moveto 200 a lineto 200 a 10 add lineto 0 a 10 add lineto closepath a 400 div 0.0 0.0 setrgbcolor fill newpath 200 a moveto 400 a lineto 400 a 10 add lineto 200 a 10 add lineto closepath 0.0 1.0 a 400 div sub 0.0 setrgbcolor fill } for showpage