## Need quick help with simple shapes

## Need quick help with simple shapes

(OP)

Ok I have only a basic knowledge of PostScript, and need a guide to a certain function.

I need to do 4-5 basic shapes inside postscript, but not with normal commands as lineto and such, but preprogrammed (I hope I'm understood, I really don't know much about this filed)

Example:

I need to preprogram the square, writing:

x 0 rlineto

0 x rlineto

-x 0 rlineto

closepath

And then, when I need the square, I would write

20 square

and the square with 20 instead of X would show up.

Please tell me if this is possible and how, or point me to some reading about it.

It really seems simple but I just need a quick step-by-step guid

Thank you

I need to do 4-5 basic shapes inside postscript, but not with normal commands as lineto and such, but preprogrammed (I hope I'm understood, I really don't know much about this filed)

Example:

I need to preprogram the square, writing:

x 0 rlineto

0 x rlineto

-x 0 rlineto

closepath

And then, when I need the square, I would write

20 square

and the square with 20 instead of X would show up.

Please tell me if this is possible and how, or point me to some reading about it.

It really seems simple but I just need a quick step-by-step guid

Thank you

## RE: Need quick help with simple shapes

/square {

5 dict begin

gsave

/x exch def

newpath

x 0 rlineto

0 x rlineto

x neg 0 rlineto

closepath

stroke

grestore

end

} def

Alot of that is overkill and is written to minimize side-effects although you lose a little flexibility. The "dict begin / end" block basically makes sure that the definition of x will not effect another x out there. The "gsave / grestore" makes sure that once you finish the square everything is as it was before except the square has been drawn - in some situations this may not be what you want. You may want to decide whether you wish to stroke or fill the square in which case you would want to take the gsave, grestore, and stroke statements out. (I would just make a second procedure with a fill, though). newpath creates a new path, but if you wish to add the square onto your existing path before calling this, you would need to take that statement out. If you do this, though, it leaves you open to side-effects.

## RE: Need quick help with simple shapes

I need square, rectangle, circle, triangle, and perhaps a romb...

Got two questions about your answer though.

1. How do you actually call the square after you preprogrammed it with that code?

After your code I wrote

20 20 moveto

10 square

showpage

And nothing appeared.

2. How would the sintax go if I would have 2 or more variables.

## RE: Need quick help with simple shapes

## CODE

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:

## CODE

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

/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.

## RE: Need quick help with simple shapes

The explaination is handy too so thanks for that, now I can proceed to doing other shapes

Thank you