% This file was converted from HTML to LaTeX with Nathan Torkington's
% html2latex program
% Version 0.9b
\chapter{Transformations}
The PostScript interpreter keeps track of a matrix called the current 
transformation matrix.  When constructing an image, the interpreter 
uses this matrix to convert the world coordinates used by the program 
into device coordinates used by the printer itself.  Generally, the 
actual contents of the matrix are of little interest to a well-written 
PostScript program; the reason for this is that the specific contents 
are device-dependent.  A program that uses them might not work 
properly.  PostScript does provide a number of operators, however, 
that transform the matrix in a device-independent way.  These 
operators allow you to transform the way user space maps onto device 
space, and they modify the current transformation matrix with a simple 
matrix transformation.  The basic transformation operators are:
\begin{itemize}
\item \code{rotate}
\item \code{translate}
\item \code{scale}
\end{itemize}

It is useful to realize that the current transformation matrix (and, 
hence the effect of all these operators) is part of the current 
graphics state and can be saved and restored using the \code{gsave}, 
and \code{grestore} operators.  In addition, the transformations on 
the matrix affect path components constructed {\em after} the 
transformation.  Even if a path is only partially constructed when a 
transformation is invoked, the parts of the path that were in place 
before the transformation will be unaffected.

\section*{Rotate}
The \code{rotate} operator takes a single, numerical operand.  This 
operand specifies how many degrees to rotate the user space around its 
origin (positive values specify counter clockwise rotations).  This 
transform allows you to do some pretty neat tricks.  For example, 
let's say you have written a routine to draw some complex shape; and 
you have found that you need to draw it several times at different 
angles.  In a more primitive graphics system, you might need to 
re-write to routine to take an angle as an argument, but in PostScript 
you only need to rotate the coordinates with the \code{rotate} 
operator.

As a concrete example, let's say you want to draw lines in a circular 
pattern so that each line is ten degrees from its neighbors.  Rather 
than figure out the coordinates for each of the 36 lines, we can just 
draw a horizontal line and rotate it repeatedly to different angles.  
To do the repeated looping, we can use the \code{for} operator.  The 
\code{for} operator takes four arguments: an initial index value, a 
step size, a final index value, and a procedure.  The operator 
increments an index from the initial value to the final value, 
incrementing it by the step size.  For each index value, \code{for} will push 
the index on the stack and execute the procedure.  This gives you a 
simple means of looping.
 
We start by setting up the \code{for} loop.  At the beginning of the 
loop's procedure, we start a new path and save the graphics state.
\begin{verbatim}
    0 10 360 {       % Go from 0 to 360 degrees in 10 degree steps
      newpath        % Start a new path
      gsave          % Keep rotations temporary
\end{verbatim}

We next set the start of the line to (144, 144) and rotate the 
coordinates, we do not rotate {\em before} moving because (144, 144) 
would then be in a different location.
\begin{verbatim}
      144 144 moveto
      rotate         % Rotate by degrees on stack from `for'
\end{verbatim}

We next draw just a horizontal line:
\begin{verbatim}
      72 0 rlineto
      stroke
\end{verbatim}

Finally, we restore the old graphics state and end the loop.
\begin{verbatim}
      grestore        % Get back the unrotated state
    } for             % Iterate over angles
\end{verbatim}

\section*{Translate}
The \code{translate} operator takes two operands: an {\it x}-coordinate, 
and a {\it y}-coordinate.  The \code{translate} operator sets the origin 
of user space to the point that was at the given coordinates in user 
space.  The main use of \code{translate} is to draw copies of a shape 
in different locations.  Typically, a shape will be constructed at the 
origin, and the shape will be translated to the correct location 
before it is to be drawn.  A simple example translates a box 
constructed at the origin to the point (72, 72) in the original user 
space.

\section*{Scale}
The \code{scale} operator takes two arguments: an {\it x} scale factor, 
and a {\it y} scale factor.  The operator scales each coordinate by its 
associated scale factor.  That is, if you have an {\it x} scale factor of 
0.5 and a {\it y} scale factor of 3, the {\it x} coordinate will be reduced by 
a factor of two while the {\it y} coordinate will be magnified by a factor 
of 3.  This operator allows you to change the size and dimensions of 
objects quite easily.
 
A simple example can just scale text in a couple of ways.

We can make things narrow:
\begin{verbatim}
    gsave
      72 72 moveto
      0.5 1 scale           % Make the text narrow
      (Narrow Text) show    % Draw it
    grestore
\end{verbatim}

We can make things tall:
\begin{verbatim}
    gsave
      72 144 moveto
      1 2 scale           % Make the text tall
      (Tall Text) show    % Draw it
    grestore
\end{verbatim}

We can distort the text completely:
\begin{verbatim}
    gsave
      72 216 moveto
      2 0.5 scale          % Make the text wide and short
      (Squeezed Text) show % Draw it
    grestore
\end{verbatim}

\section*{Combining Transformations}
Each of these transformations merely modifies the current
transformation matrix.  This means that these operators can be 
combined for some interesting effects.  For example, you can take a 
normal document and print two of its pages on a single page (reduced 
and placed side-by-side) simply by translating the first page to one 
side, rotating the page by ninety degrees and then reducing the page 
so that it fits.  The second page is handled in the same manner, but 
is translated to the other side of the page.  This can be easily done 
by PostScript postprocessors so long as they know where one page ends 
and the next begins (this is often accomplished using special 
comments).  A somewhat simpler example is to draw a simple box and 
some text translated, rotated, and scaled in various ways.  An 
important thing to remember when viewing this example is that 
translations are always relative to the {\em current user space}.  
This means that
\begin{verbatim}
    0.5 0.5 scale
    72 72 translate
\end{verbatim}
will have a different effect on the image than does
\begin{verbatim}
    72 72 translate
    0.5 0.5 scale
\end{verbatim}

In the first case, the origin will be half an inch in from the bottom 
and left margins.  In the second case, the origin will be an inch in 
from the two margins.
