Graphics Transformations With Postscript
In this tutorial, we will preform multiple transformations on graphics. A text editor will be used to write postscript (notepad is sufficient). GSview will also be used to view our files.

How Transformations Work

So far we have done all our PostScript programming using the Default User Coordinate System. Today we will be applying transformations to the coordinate system. These transformations effect everything we do from the time we apply them.

The following procedures will be used to manipulate the position, size, and   orientation of the coordinate system:

Command Function

translate

changes the position of the coordinate system

rotate

rotates the coordinate system by degrees

scale

compresses or expands the x and/or y axis

We will also be using methods to save and restore the state of the coordinate system:

Command Function

gsave

saves state

grestore

restores state


Translate

First lets set the font and create a procedure that will print something out:

%!

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Playing with transformations%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

/Times-Bold findfont 30 scalefont setfont

/ps
{0 400 moveto (PostScript is #1) show } def

Now we will use a repeat loop to show the application of the translation:

8
{ps
50 48 translate} repeat
ps

showpage
Now move to 0,0 and try to print something and see where is ends up.

Rotate

Try the following rotation

/ps
{250 350 moveto (PostScript is #1) show } def

8
{ps
5 rotate} repeat
ps

showpage
Notice two things about the rotation:
1. It moves counter-clockwise
2. Try starting your function 'ps' from the origin. (the rotating is done there)



Scale

Scale changes the coodinates where you are working as well as the size of what you are working with.

/ps
{0 75 moveto (PostScript is #1) show } def

8
{ps
1.3 1.3 scale} repeat
ps

showpage

Try using different numbers for non-uniform scaling
0 0 moveto
.1 .5 scale
(YeeeeHaaawww!!!!) show

Putting It All Together

The three transformation function can be powerful graphically when combined. Especially when used in combination with gsave and grestore. Lets try a simple combination.


%!

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Playing with transformations%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

/Times-Bold findfont 30 scalefont setfont

/ps
{0 400 moveto (PostScript is #1) show } def

100
{ps
48 50 translate
2 rotate
.9 .9 scale} repeat
ps

showpage

Now lets create a couple of related procedures to use:

/ps2
{200 400 moveto (PostScript is #1) show } def

/ps3
{400 400 moveto (PostScript is #1) show } def
But if we use them as they are, the state of the coordinate system will just pick up where it left off and we will just be printing out very tiny letters. So, we need to insert gsave and grestore command at the appropriate places:
%!

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Playing with transformations%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

/Times-Bold findfont 30 scalefont setfont

/ps
{0 400 moveto (PostScript is #1) show } def

/ps2
{200 400 moveto (PostScript is #1) show } def

/ps3
{400 400 moveto (PostScript is #1) show } def

gsave

100
{ps
48 50 translate
2 rotate
.9 .9 scale} repeat
ps

grestore
gsave %We have to save the state again because gsave setting were popped off the stack.

100
{ps2
48 50 translate
2 rotate
.9 .9 scale} repeat
ps2

grestore

100
{ps3
48 50 translate
2 rotate
.9 .9 scale} repeat
ps3

showpage % We're done... eject the page

Try this code out!

The PostScript Tutorial Concieved and Created by Dann Ormond & Will Munn. Inspired by Mike Grady PhD. Website Design by NiftySites.com.