Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need quick help with simple shapes 1

Status
Not open for further replies.

ConeShape

Technical User
Nov 30, 2006
3
HR
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
 
This will do the trick:

/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.
 
I don't need to add shapes to anything, simply create them with certain side lenghts
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.

 
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:

Code:
10 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 which will give you a tool that is invaluable for playing around with postscript.
 
This is exactly what I needed, and I'm eternaly grateful man

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

Thank you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top