## CMSC 15100 - Autumn 2005 Lab 2

Extra Credit problems are in red!!

### 0. Designing Functions

In this lab you will be introducing conditional and compound data types. You must follow the Design Recipe appropriate for each function. This includes

1. Data Analysis (for conditional and compound data)
3. Design template (for conditional and compound data)
4. Examples which establish your function works on the range of cases it was designed for. You must prove to me your function works!!
Below are links which explain the Design Recipe for each of the functions you will design in this lab

### 1. Conditional Data Definitions

A shape is one of: circle, ellipse, square, rectangle or triangle. Write a function which takes a shape and three colors and produces three concentric shapes using each of the three colors. You will produce an image like:

### 2. Compound Data Definitions

We want to manipulate shapes, such as draw them, and compute their area. A shape will still include one of circle, ellipse, square, rectangle or triangle, but now you will need to draw them and compute their area, so you will need structures.

1. Define structure(s) for handling shapes. You may try to design a single shape structure, or several structures for the different kinds of shapes. You must document your structure:
• Data Contract: which explains the appropriate values for each field.
• Data Definition: see structure definitions for how to define structures for compound data in Scheme.
• List of all constructors, selectors, and predicates introduced by the structure definition.
2. Write individual functions which will draw each shape, such as ` draw-circle `. You must have a different function for each shape, regardless of how you chose to represent shapes.
3. Write individual functions which compute the area of each shape, such as `area-circle`.
• ellipse: 3.14 * (width/2) * (height/2)
• rectangle: width * height
• square: side * side
• triangle: (0.433 * side * side
The formula is actually (sqrt(3)/4) * side * side

### 3. Mixing Conditional and Compound Data

A shape is a circle, ellipse, rectangle, square, or triangle, where each of these were defined in Part 2. Write two functions:

1. `draw-shape`: draws a shape.
2. `area-shape`: computes the area of a shape.

### 4. Right

Recall that an image is a box of dimensions `image-width` by `image-height` containing colored pixels. Write the following function:

``` ;;; right: image image -> image ;;; Position second image to right of first image. The pinhole of the ;;; resulting image is the same place as the pinhole in the first image. ``` You may not assume the pinhole is centered, and you may not change the pinhole.

### 5. Sorting Shapes

Write the following function:

• `sort-shapes`: Given two shapes, draws the smaller to the right of the larger.

### 6. Checkerboard

Write the following function:

``` ;;; checkerboard: color color -> image ;;; Produce an 8x8 checkerboard pattern of alternating colors ```

which produces an image like

Avoid Copy Code (i.e. duplicate code!!) Break-up the problem into smaller, auxillary problems and piece these together to produce the checkerboard image. You will find it useful to write a function such as `above` or `below` as with `right` from section 4.