Skip to main content Accessibility help
Internet Explorer 11 is being discontinued by Microsoft in August 2021. If you have difficulties viewing the site on Internet Explorer 11 we recommend using a different browser such as Microsoft Edge, Google Chrome, Apple Safari or Mozilla Firefox.

Chapter 4: Shapes II: Drawing Shapes

Chapter 4: Shapes II: Drawing Shapes

pp. 48-55

Authors

, Yale University, Connecticut
  • Add bookmark
  • Cite
  • Share

Summary

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:

About the book

Access options

Review the options below to login to check your access.

Purchase options

eTextbook
US$79.00
Hardback
US$182.00
Paperback
US$79.00

Have an access code?

To redeem an access code, please log in with your personal login.

If you believe you should have access to this content, please contact your institutional librarian or consult our FAQ page for further information about accessing our content.

Also available to purchase from these educational ebook suppliers