Go to the first, previous, next, last section, table of contents.

Graphical transexampleions

Another type of transexampleion is graphical, where you want to change or set the graphical attributes that a particular picture is to be drawn with. For example, to create a filled, green circle for our traffic light:


greenCircle :: Unit -> Picture
greenCircle rad =
 Pen [PenForeground green,
      PenFill True] $
 circle rad

we apply the Pen constructor to a picture describing a simple circle. The constructor returns a new picture by adding a set of PenModifier to a Picture:

@tindex{PenModifier}


data Picture =
 ...
 | PicPen PenModifier Picture
 ...

such that when the circle is rendered, the graphical attributes specified in the PenModifier value will be used, i.e., that the foreground colour should be green and that the closed area the circle describes should be filled in. The PenModifier value in the PicPen constructor consist simply of a list of graphical attributes, see Appendix See section Complete picture type for complete list.

The elements of the PenModifier list gives you a fine-grained control over how a picture is drawn, but sensible defaults are defined for all values, so the Pen constructor is only required if you want to override these values.

In the case of nested applications of the Pen constructor, PenModifier attributes are `lexically' scoped, i.e., the (attribute,value) pair set in an application of Pen overrides any previous value set for that attribute. To illustrate the scoping rule, when the following Picture is rendered

\hbox{ \hspace{0.35in} \begin{minipage}[b]{2.5in} \begin{verbatim} picture = Pen [PenFill False, PenForeground black] $ Pen [PenFill True, PenForeground grey80] $ circle 30 \end{verbatim} \end{minipage} \vbox{\vspace*{0.4in} \psfig{figure=pen-attr.eps}}}

picture =
 Pen [PenFill False,
      PenForeground black] $
 Pen [PenFill True,
      PenForeground grey80] $
 circle 30

Pen attributes

the picture on the right should be displayed. When the circle is rendered, the foreground colour will be grey80 and the circle should be filled, since the innermost application of Pen overrides the attribute values of the outer application.

Note that the graphical attribution done by Pen creates a new Picture value, and avoids having to use some shared, mutable graphics state. The graphical transformer, Pen, simply associates a set of graphical attribute values with a picture.

Representing the three lights in the traffic light becomes now just:


filledCircle :: Colour -> Unit -> Picture
filledCircle col rad = 
 Pen 
   [PenForeground col,
    PenFill True] (circle rad)

redLight, orangeLight, greenLight :: Unit -> Picture
redLight    = filledCircle (red::Colour)
orangeLight = filledCircle (orange::Colour)
greenLight  = filledCircle (green::Colour)


Go to the first, previous, next, last section, table of contents.