Argh, you are absolutely correct. If I am not going to specify the origin, which I did not in the example I gave you, the newpath statement should not be in there. The newpath effectively clears the path which means it can't use the 20,20 you specified with the moveto as part of the path. I just ran the following and it probably behaved exactly as would expect/ want: CODE/square { 5 dict begin gsave /x exch def x 0 rlineto 0 x rlineto x neg 0 rlineto closepath stroke grestore end } def
20 20 moveto 10 square showpage 2) Basically, postscript uses a stack to maintain its variables and then certain statements will pop elements off the stack and push results back on the stack. For instance, here is a line by line blow of why 10 square works: CODE10 square % pushes 10 onto the stack and executes square since we have defined it: 5 dict begin % assigns a new dictionary and allocates space for 5 entries gsave % stores the current state of graphics for later retrieval (would store current point/path information, etc.) /x % Pushes /x onto the stack - stack is now: 10 /x exch % Switches last two elements of stack, now: /x 10 def % def takes last two elements and assignes them, clearing the stack and making x=10 in the dictionary. x 0 rlineto % Puts 10(what x now is) and 0 and executes rlineto which clears the stack and draws the line 0 x rlineto % Similiar x neg 0 rlineto % Similar note that "x neg" pops x and pushes -x closepath % close current path stroke % draw current path grestore % restore graphics before all this started end % restore dictionary (effectively makes x a local variable) This may be more than what you wanted to know, so here is a quick example of a rectangle but you can trace through it and see how it works: CODE% Usage: x y rectangle - strokes a rectangle with width x and height y from the current point
/rectangle { 5 dict begin gsave /y exch def %note the reverse order, y will be popped first /x exch def x 0 rlineto 0 y rlineto x neg 0 rlineto closepath stroke grestore end } def
% Example 100 100 moveto 10 100 rectangle showpage If you do not have ghostscript, look at http://www.cs.wisc.edu/~ghost/ which will give you a tool that is invaluable for playing around with postscript. |
|