CS 3723/3721
Programming Languages

Practice With Graphics

 


Practice with translate, rotate, and scale: Consider the Postscript code below that lays out coordinate axes and numbers the four quadrants (1, 2, 3, 4). The function that does this leaves the origin at (0, 0). In order to show the figure, the code below translates the origin to (200, 200). This code also puts a standard border around the page. The following examples will only supply new code for the last few lines (shown in red below).

Postscript Program
%!PS-Adobe-2.0
/width 25 def
/outline { % print standard border on page
   /Times-Bold findfont 15 scalefont setfont
   newpath width width moveto
   0 792 width 2 mul sub rlineto
   612 width 2 mul sub 0 rlineto
   0 -792 width 2 mul add rlineto
   closepath stroke
   275 width 5 add moveto (Bottom of Page) show
   275 792 width sub 15 sub moveto
   (Top of Page) show
} def

/coord {
   /Times-Bold findfont 50 scalefont setfont
   20 20 moveto (1) show
   -50 20 moveto (2) show
   -50 -50 moveto (3) show
   20 -50 moveto (4) show
   3 setlinewidth
   newpath 0 60 moveto 0 -60 lineto stroke
   newpath 60 0 moveto -60 0 lineto stroke
} def
outline
%%%%%%% only change after this comment %%%%%%
200 200 translate
coord
   
showpage
Result in: .ps, or as .pdf

Now keeping the same functions outline and coords, try some different instructions at the end and see what happens. In each case try to guess what the result will be first and then see what the result is. (I am leaving off the definition of the function and leaving off the final showpage.)

Substitute for red code Result
200 200 translate
rotate 90
coord
.ps, or as .pdf
rotate 90
200 200 translate
coord
.ps, or as .pdf
90 rotate
200 -200 translate
-90 rotate coord
.ps, or as .pdf
200 200 translate
90 rotate
2 2 scale
coord
.ps, or as .pdf
2 2 scale
200 200 translate
90 rotate
coord
.ps, or as .pdf
1 1 30 {
  pop % throw away loop variable
  300 150 translate
  45 rotate
  coord
  0.75 0.75 scale
} for % just loops 30 times
.ps, or as .pdf

( Revision date: 2015-01-03. Please use ISO 8601, the International Standard Date and Time Notation.)