- Language: Beginning Student
- Teachpack: image.ss
- Lab 2 Template (scm)
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
- Data Analysis (for conditional and compound data)
- Contract, Purpose, Header
- Design template (for conditional and compound data)
- Examples which establish your function works on the range of cases it was designed for. You must prove to me your function works!!
- Functions using simple data objects
- Functions using conditional data objects
- Functions using compound data objects
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.
- 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.
- 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. - Write individual functions which compute the area of each shape,
such as
area-circle.- circle: 3.14 * radius * radius
- 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:
-
draw-shape: draws a shape. -
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
You may not assume the pinhole is centered, and you may not change the pinhole.
;;; 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.
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.