In Chapter 2 a shape data type and a function for computing the area of shapes were defined. In the last chapter you learned about basic graphics programming in Haskell. In this chapter I will define another function of shapes, namely one that converts a shape into a graphics value that can then be drawn in a graphics window. Conceptually, this function is no different from the area function defined in Chapter 2. In both cases, a shape is turned into some other kind of value; in the case of area, that value has type Float, and in the case of the function to be defined in this chapter, that value has type Graphic.
In order to perform graphics IO, we need to import the graphics library, as discussed in the last chapter. Additionally, we need to import the Shape module. Calling our new module Draw, we therefore write:
module Draw (inchToPixel, pixelToInch, intToFloat, xWin, yWin, trans, shapeToGraphic, spaceClose) where
import Shape
import SOEGraphics
where the list of names contains those functions and values, inchToPixel, pixelToInch, etc., that we choose to export from the module, and that are defined in the remainder of this chapter.
Dealing With Different Coordinate Systems
Before proceeding, let's define a couple of coercion functions that we will use to convert, or “coerce,” the coordinates of a graphics window into ones that we are more familiar with (and vice versa).
In our discussions of shapes, we have always assumed floating-point numbers for all dimensions, presumably in inches or some similar dimensional units. But the Graphics Library uses pixel coordinates, so first we need a function to convert from the former to the latter. Let's assume that the floating-point numbers are in inches, and that there are 100 pixels per inch. Thus, to convert from inches to pixel coordinates, we can apply the following function: