
                          [42m Learning PostScript[0m

                              [42m ~ Part 5 ~[0m

                            [3m By Peter Goed[0m



[32m   <> 34 <%> 34 <%> 34 <%> 34 <%> 34 <%> 34 <%> 34 <%> 34 <%> 34 <%> 34 <>
[0m

   [33mArcs and Curves

   [33mDrawing Curves
[0m
   In this tutorial we will look at the simplest and most difficult graphic
  shapes that can be constructed using PostScript; namely arcs and curves.
  These are the most versatile PostScript operators.

   The 'arc' operator is one of the simplest of all the PostScript operators
  for drawing graphic shapes.  It is much easier to draw circles using the
  'arc' operator than it is to construct rectangles.

   The 'arc' operator draws arcs of any length or radius at any position.
  You can use it in conjunction with 'lineto' to draw semicircles or pie
  wedges of any radius or orientation, 'fill' and 'stroke' can be used to
  paint the resulting shape with any colour.  When used in conjunction with
  'scale', the 'arc' operator can also be used to draw an ellipse.

   A variation of the 'arc' operator, 'arcto' may be used to connect
  intersecting lines with curved line segments, and although more difficult
  to use than the 'arc' operand, is ideal when constructing rectangles with
  rounded corners.

   The 'curveto' operator is the most powerful PostScript operator and also
  the hardest to use.  At the end of this tutorial we will briefly discuss
  'curveto' and in tutorial 7 we will use this operator to design some
  elaborate symmetrical designs.


  [33m 'arc'
[0m
   In Tutorial 1 we constructed a simple rectangle using five PostScript
  operations; one 'moveto', three 'lineto' or 'rlineto' and a 'closepath'
  instruction.  Compared to this circles are a piece of cake.

   A circle can be of any size at any location and can be easily drawn using
  a single 'arc' operator, together with its five operands.  These five
  operands describe in order, the x and y coordinates for the centre of the
  circle, the radius of the circle and the beginning and ending angles of the
  arc.  For circles the beginning angle is 0 degrees and the ending angle is
  360 degrees.

   The following example will produce a circle of 2 inch radius centred on
  the page.

   306 396 translate    % sets the point of origin at the page centre
   0 0 144 0 360 arc    % the 0,0 coordinates set the x and y coordinates
                        % at the "translated" centre of 306, 396
   stroke

   showpage


  [33m Donut
[0m
   Using the same circle as previously, we now use the 'fill' operator to
  fill the 2 inch circle with the default colour (black) and the 1 inch
  radius circle with white.  Because PostScript painting operators apply
  opaque paint, the inside circle obscures the portion of the black circle
  underneath, thus creating a "donut" effect.

   306 396 translate
   0 0 144 0 360 arc
   fill

   1 setgray
   0 0 72 0 360 arc
   fill

   showpage


  [33m Yin and Yan
[0m
   The following code produces in a fairly simple way, a very complex graphic
  that would be hard to draw by hand.

   The Yin and Yan symbol is created using only six 'arc' operators, five of
  which are circles and one a semicircle.  One of the arcs is stroked and
  five are filled - three with black and two with white.

   The page description for this example must be constructed in the proper
  sequence for it to come out correctly.

   306 396 translate      % set page centre as 0,0

   0 0 144 90 270 arc     % step one; black semicircle
   fill                   % with black (default)

   0 -72 72 0 360 arc     % step two; lower black circle
   fill

   gsave                  % step three, draw two white circles
       1 setgray          % set colour to white
       0 72 72 0 360 arc  % upper white circle
       fill               % paint white
       0 -72 12 0 360 arc % white dot
       fill
   grestore

   0 72 12 0 360 arc      % step four, black dot
   fill
   0 0 144 0 360 arc      % black outer circle
   stroke

   /Times-Roman findfont 20 scalefont setfont
   -60 -200 moveto
   1 2 scale
   (Yin and Yan) show

   showpage


   There is no need to use 'closepath' to finish the semicircle in step one.
  The use of the 'fill' command tells the interpreter to to close the path
  implicitly.



  [33m Semicircle
[0m
   The following code will construct a semicircle from 90 degrees to 270
  degrees, fill the semicircle with the default colour, draw a circle around
  the semicircle and show the cardinal compass points.

   306 396 translate
   /Helvetica-Bold findfont 24 scalefont setfont
   0 0 72 90 270 arc
   fill

   .1 setlinewidth
   0 0 144 0 360 arc
   stroke

   110 -5 moveto
   (0/360\312) show   % the "\nnn" is used to access characters from  the
                      % expanded character set.  Here we used "octal"
                      % \312, which gives us the degree symbol.  For a
                      % further explanation, see the PostScript Language
                      % Reference Manual, pages 254 and 255 give a full
                      % listing of the character set and its "octal"
                      % numbers.
   -15 138 moveto
   (90\312) show
   -165 -5 moveto
   (180\312) show
   -20 -150 moveto
   (270\312) show

   showpage


   [33mPie Wedge
[0m
   The following instruction will create a pie wedge comprising 20% of a
  circle.  The arc begins at 0 degrees and ends at 72 degrees.

   306 396 translate
   /Times-Bold findfont 36 scalefont setfont
   2 setlinewidth
   0 0 144 0 72 arc     % 360 degrees x 20% = 72 degrees
   0 0 lineto
   closepath
   gsave
       fill
   grestore
   stroke

   gsave
       1 setgray
       35 30 moveto
       (20%) show
   grestore

   gsave
       /Helvetica-Bold findfont 24 scalefont setfont
       150 -5 moveto % you will need to remove these two  instructions
       (0\312) show  % when completing the whole Pie Chart in part three
       35 144 moveto
       (72\312) show
   grestore

   showpage    % used here only to print out part one of this code.
               %  Remove to allow parts one and two to function together.


  [33m Second Pie Wedge
[0m
   We can issue the following instructions to create a second pie wedge to
  our page instructions.

   % continued from previous code before the 'showpage' instruction.
   0 0 144 72 arc   % 360 degrees x 35% = 126 degrees + 72 degrees =
   198
   0 0 lineto
   closepath
   gsave
       .9 setgray
       fill
   grestore
   stroke
   -90 40 moveto
   (35%) show

   gsave
       /Helvetica-Bold findfont 24 scalefont setfont
       -200 -50 moveto
       (198\312) show
   grestore

   showpage    % don't forget to remove this instruction to enable
               % part three to function together with parts one and two.


   Complete Pie Chart

    % continued from previous code before the 'showpage' instruction.
    0 0 144 198 360 arc   % 360 x .45 = 162 + 198 = 360
    0 0 lineto
    closepath
    stroke
    -20 -80 moveto
    (45%) show
    gsave
        Helvetica-Bold findfont 24 scalefont setfont
        150 -5 moveto
        (0/360\312) show   % don't forget to remove the "(0\312) show
    from part one of the Pie Wedge exercise.
    grestore

    showpage


  [33m Pie Slice
[0m
   The versatility of PostScript makes it very easy to "slice" a piece of pie
  to emphasise any of the pie wedges.  This is easily accomplished by adding
  a 'gsave/grestore' around the wedge and using translate to reposition the
  pie slice.

   I have added a few new instructions to the previous code and removed the
  degree markings around the outside of the pie.

    306 396 translate
    /Times-Bold findfont 36 scalefont setfont
    2 setlinewidth
    gsave                 % added
        25 20 translate   % added
        0 0 144 0 72 arc  % 360 degrees x 20% = 72 degrees
        0 0 lineto
        closepath
        gsave fill grestore
    stroke
    gsave
        1 setgray
        35 30 moveto
        (20%) show
        grestore
    grestore              % added

    gsave                 % added
        0 0 144 72 arc    % 360 degrees x 35%=126 degrees + 72 degrees=198
        0 0 lineto
        closepath
        gsave .9 setgray fill grestore
        stroke
        -90 40 moveto
        (35%) show
    grestore              % added

    gsave
        newpath           % added because there was no explicit 'moveto'
                          % instruction preceding the 'arc' instruction.
                          %  To see the difference, remove the 'newpath'
                          % instruction and see the difference that it
                          % creates.
        0 0 144 198 360 arc  % 360 x .45 = 162 + 198 = 360
        0 0 lineto
        closepath stroke
        -20 -80 moveto
        (45%) show
    grestore

    showpage


  [33m Adding Labels to Your Pie Chart
[0m
   If you remove the 'showpage' instruction from the previous example and add
  the following instructions, you will produce an attractive pie chart
  complete with descriptive labels.  This is a very handy piece of code and
  can be reused for many business applications by minor manipulation of this
  code to suit a specific set of circumstances.

    gsave
        /Helvetica findfont 24 scalefont setfont
        150 110 moveto
        (FEES) show
        -215 150 moveto
        (SALES TAXES) show
        -90 -180 moveto
        (INCOME TAXES) show
    grestore

    -175 250 moveto
    2 2 scale
    (REVENUE) show

    showpage

  [33m Transforming Circles into.....
[0m
   You can easily transform circular shapes into elliptical shapes by scaling
  them nonuniformly.  Firstly let us create four shaded circular shapes.

    306 396 translate

    0 252 translate
    0 0 72 0 360 arc stroke

    0 -180 translate
    0 0 72 0 360 arc fill

    0 -180 translate
    gsave
        0 0 72 0 360 arc
        .5 setgray
        fill
    grestore

    0 -180 translate
    0 0 72 0 180 arc closepath
    gsave
        .9 setgray fill
    grestore
    stroke

    showpage


  [33m Ellipses
[0m
   We now use the 'scale' operator to stretch our four circular shapes into
  various elliptical shapes.  Some experimentation is encouraged to get the
  feel of this section.

    306 396 translate

    0 252 translate
    gsave
        .5 1 scale
        0 0 72 0 360 arc stroke
    grestore

    0 -180 translate
    gsave
        2 1 scale
        0 0 72 0 360 arc fill
    grestore

    0 -180 translate
    gsave
        .25 .50 scale
        0 0 72 0 360 arc
        .5 setgray fill
    grestore

    0 -180 translate
    gsave
        4 1 scale
        0 0 72 0 180 arc closepath
        gsave
        .9 setgray fill
        grestore
        stroke
    grestore

    showpage


  [33m Atomic Patterns
[0m
   We can combine 'arc' and 'scale' with 'rotate' to change the orientation
  of each ellipse to create an interesting construction for the atom.

    306 396 translate
    gsave
        0 rotate
        .33 1 scale
        0 0 144 0 360 arc stroke
    grestore

    gsave
        60 rotate
        .33 1 scale
        0 0 144 0 360 arc stroke
    grestore

    gsave
        -60 rotate
        .33 1 scale
        0 0 144 0 360 arc stroke
    grestore

    showpage


   Ellipses with Text

   You can draw an ellipse at any location specified in a 'translate'
  instruction.  Always use 0, 0 for the x/y coordinates for the 'arc'
  operator when drawing ellipses.  Otherwise the 'scale' operator used for
  the ellipse will relocate the ellipse elsewhere on the page.

    /Times-BoldItalic findfont 72 scalefont setfont
    gsave
        204 666 translate
        2 .9 scale
        0 0 72 0 360 arc fill
    grestore

    gsave
        204 666 translate
        2.1 1 scale
        0 0 72 0 360 arc stroke
    grestore

    1 setgray
    72 648 moveto
    (Typeface) show

    showpage


  [33m 'arcto'
[0m
   The 'arcto' operator connects two line segments with a circular arc of any
  radius and angle.  It accomplishes this by adding an arc to the end of one
  line segment, from which an other line can be added.

   This makes 'arcto' ideal for connecting two line segments with rounded
  corners which can then be extended to form either a rectangle with rounded
  corners or two parallel lines connected with rounded ends.

   The five operands required by 'arcto' are the x and y coordinates tangent
  to the arc on both the first and the second line, and the radius of the arc
  (x1 y1 x2 y2 r).  We use the 'clear' operator here to discard some residual
  information generated by the PostScript interpreter after executing
  'arcto'.

   Whether or not the arc is preceded by a straight-line segment is
  determined by the distance between the points and the radius of the arc.
  Arcs both with and without straight line segments are used in the following
  example, in which two parallel lines are connected by curved ends.

    108 108 translate
    newpath                  % sets a new current point to prevent an
                             % error code being generated

    0 36 moveto              % moves the current point up 36 points on the
                             % page to give us starting coordinates for
                             % x0 (108) and y0 (144)

    0 72 216 72 36 arcto clear    % 0=x1 72=y2 (x1, y1 axis point set
                                  % at 108, 180 [108+72=180])
                                  % 216=x2 72=y2 (x2, y2 axis point set at
                                  % 324, 180 [108+216=324, 108+72=180])
                                  % from the x1, y1 and x2, y2 coordinates
                                  % above, the starting point for our
                                  % radius will be at the only point
                                  % located at a distance of 36 points (r)
                                  % in a direction perpendicular to both
                                  % tangent lines and is located within
                                  % the inner angle between the tangent
                                  % lines, this point can only fall at
                                  % 144, 144, and an arc of radius 36 is
                                  % drawn from 108, 144 to 144, 180.

    216 72 216 0 36 arcto clear   % x1,y1=324, 180  x2,y2=324, 108 and
                                  % hence our radius centre point will be
                                  % at 288, 144.  This instruction will
                                  % now draw a straight line from 144, 180
                                  % to 288, 180 and then draw an arc from
                                  % this point to 324, 144.

    216 0 0 0 36 arcto clear      % x1,y1=324, 108  x2,y2=108, 108 and the
                                  % radius point will be at 288, 144 [take
                                  % into account where our last radius
                                  % ended, as this is the current point
                                  % this arc will start from.  The
                                  % endpoint for the arc is at 324, 144]
    0 0 0 72 36 arcto clear       % x1,y1=108 ,108 x2,y2=108, 180 and
                                  % the radius point will be 144, 144.
                                  % A line will now be drawn from the end
                                  % of the previous arc at 288, 108 to the
                                  % start of this arc at 144, 108, and the
                                  % arc will finish at 108, 144 - which
                                  % was our starting point of the 1st arc.
    stroke

    showpage

   I suggest that you look at the explanation given in the PostScript
  Language Reference Manual (the red book) on page 119 for a more detailed
  description of 'arcto', as this instruction is fairly hard to come to terms
  with.  It is a good idea to draw this example out on a sheet of paper
  marked with a 72 point line grid to get a better understanding, I found
  that this helped no end.


  [33m Round Box with Text
[0m
   You can combine rounded boxes created with 'arcto' with text to create a
  pleasing title page, as shown by the following page description.

    /Times-BoldItalic findfont 72 scalefont setfont
    76 424 moveto
    76 472 528 472 48 arcto clear
    528 472 528 376 48 arcto clear
    528 376 76 376 48 arcto clear
    76 376 76 472 48 arcto clear
    gsave
        fill      % black box
    grestore
    gsave
        12 setlinewidth
        stroke    % additional 12 point black box around perimeter
    grestore
    1 setgray
    gsave
        4 setlinewidth
        stroke    % white ring around original perimeter
    grestore

    100 400 moveto
    (ROUNDBOX) show

    showpage

   In tutorial seven we will discuss how to create a similar rounded box
  around any string of text of any size and typeface.


  [33m 'curveto'
[0m
   The 'curveto' operator is the foundation of the PostScript language, it
  gives you access to the Bezier curves discussed in tutorial 2.  This
  powerful operator adds a Bezier curve section to the current path between
  the current point and the end point of the curve.

   The 'curveto' operator requires an initial current point and six operands.
  The initial 'moveto' (new current point) establishes the beginning of the
  curve, and the last set of coordinates establishes the end of the curve.
  The other two sets of x/y coordinates are the control points required to
  shape the curve.

   This section is not an easy one to come to terms with and will require a
  bit of work with pencil and paper on a page layout graph to fully
  comprehend.  The PostScript Language reference Manual (page 140) has a
  fairly good explanation that may help you better understand this operator.

   The page description below will give you two Bezier curves, each has very
  similar code.  Note the effect that changing the 150 to -150 has on the
  curve.  Some experimentation with changing the sets of coordinates will
  help to make the picture clearer.

   The 'curveto' operator expects input as follows "x1 y1 x2 y2 x3 y3
  curveto"

    306 396 translate
    gsave
        0 144 translate                 % set centre point at 0, 144
        -200 0 moveto                   % set new current point -200, 144
        -100 100 100 150 200 0 curveto  % x1=-100 y1=244 x2=100 y2=294
                                        % x3=200 y3=144
        stroke
    grestore

    gsave
        0 -144 translate                % set centre point at 0, -144
        -200 0 moveto                   % set new current point -200, -144
        -100 100 100 -150 200 0 curveto % x1=-100 y1=-44 x2=100 y2=-294
                                        % x3=200 y3=-144
        stroke
    grestore

    showpage


  [33m Brush Stroke Underline
[0m
   The Bezier model is ideally suited for use in interactive graphics,
  because the curve can be easily manipulated by moving the control points
  with a mouse, but the 'curveto' operator is very difficult to use directly
  in a program, which is why most PostScript language tutorials say very
  little about this operator.

   However, you can use the 'curveto' operator to perform some functions that
  could not be done any other way, such as simulating a brush stroke
  underlining of text, as the following page description will show you.

    /Times-BoldItalic findfont 72 scalefont setfont
    72 648 moveto (Typeface) show
    4 setlinewidth

    77 605 moveto
    144 620 216 630 325 620 curveto

    78 606 moveto
    145 620 216 632 327 624 curveto

    78 607 moveto
    145 621 217 634 329 626 curveto

    stroke

    showpage


   As you can see it is not easy, but with a little bit of thought you can
  create some stunning underlines which cannot be done in any other way
  except a structured drawing program (maybe).


  [33m Sine Wave
[0m
   There is however one area in which 'curveto' is easier to use directly
  than it is interactively: for creating symmetrical designs where a single
  'curveto' that creates the simple sine wave is repeated many times.

    306 396 translate
    0 0 moveto
    72 72 72 -72 144 0 curveto
    stroke

    showpage

   In the next tutorial we will delve into repeat loops to create some
  wonderful symmetrical graphics patterns, we will also introduce a new
  painting operator in this tutorial, as well as a host of new and exciting
  operators.

   Again this month there will be no homework, but make sure that you
  understand the concept of the operators discussed in this month's tutorial
  and try out some examples of your own; the easiest way to do this is to use
  the "POST" interpreter from the Fish Disk Library to display your page
  descriptions on screen and/or send them to any dot matrix printer.

   From here on in we are starting to get to some fairly complex coding and
  it would be a good idea to save the majority of the code for later use in a
  more serious page description that you will no doubt want to write.  I save
  mine with a name that describes the function of the code and give it a .par
  cod to identify it as a piece of "stand alone" code that I can send out the
  parallel port to my PostScript printer and get a result.

   See you next tutorial.



  [32m <> 34 <%> 34 <%> 34 <%> 34 <%> 34 <%> 34 <%> 34 <%> 34 <%> 34 <%> 34 <>
[0m

