How to make pictures using postscript programs (part 5) ----------------------------------------------------------------------------- All arguments to commands in postscript are passed by putting them on the stack, and taking them of the stack. Each time the postscript interpreter reads a number, it puts the number on the stack; each time it reads a command, it takes as many numbers from the stack as the command needs. So moveto and lineto both need two numbers, which have to be on the stack when the command occurs, but it does not matter when they are put there. Thus newpath 50 50 moveto 200 100 lineto stroke newpath 200 100 50 50 moveto lineto stroke 200 newpath 50 50 moveto 100 lineto stroke all have the same effect: moveto finds 50 50 on top of the stack, and lineto finds 200 100. ----------------------------------------------------------------------------- You can compute with the numbers on the stack; the command add takes the top two numbers from the stack, and puts their sum on the stack. The basic arithmetic operations are add sub mul div each of these takes two numbers from the stack, and puts the sum/difference/.. on the stack. Further important functions are sin cos sqrt each of which takes one number from the stack, and computes the sine, cosine, or square root; and neg, which switches the sign (+/-). ----------------------------------------------------------------------------- The for-loop not only executes the body many times, but each time puts a new number on the stack, as specified by its three leading parameters: start, increment, and end value. So a loop 5 20 100 {..body..} for executes as 5 {..body..} 25 {..body..} 45 {..body..} 65 {..body..} 85 {..body..} If you do not use the numbers put by the loop on the stack, they just stay there. You can discard the top number from the stack by the command pop -----------------------Example Program 10------------------------------------ %!PS-Adobe-2.0 %%BoundingBox: 0 0 500 500 20 20 480 {newpath 50 moveto %this takes one number generated by the for loop 480 480 lineto stroke %and connects it to the point 480 480 } for showpage ----------------------------------------------------------------------------- That program connects each of the points 20 50, 40 50, 60 50, ...,480 50 to the point 480 480. The first coordinate is produced by the for loop. ----------------------------------------------------------------------------- You can duplicate the top item on the stack by dup and exchange the top two items by exch. -----------------------Example Program 11------------------------------------ %!PS-Adobe-2.0 %%BoundingBox: 0 0 500 500 20 20 480 { dup %duplicates the number generated by the for loop newpath 50 moveto %takes one copy of number generated by the for loop 480 480 lineto stroke %and connects it to the point 480 480 newpath 480 moveto %takesother copy of number generated by the for loop 20 50 lineto stroke %and connects it to the point 20 50 } for showpage -----------------------------------------------------------------------------