×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Need quick help with simple shapes

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

RE: Need quick help with simple shapes

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.

RE: Need quick help with simple shapes

(OP)
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.

RE: Need quick help with simple shapes

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

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close