Assignment 4
Due Date Tuesday 5/21 at 10:00pm
Purpose To design functions for itemization data.
Graded Exercises
Write enough check-expects for these exercises so that when you hit "run" no black text appears. Make sure to follow the Design Recipe!
Below is a data definition for a class of shapes.
; A Shape is one of: ; - Circle ; - Square ; - Rectangle (define-struct circl (x y r outline c)) ; A Circle is a (make-circl Number Number Number Boolean Symbol) ; interpretation: x and y determine the center of the circle, ; r the radius, outline whether it's outlined or solid, ; and c its color (define-struct squar (x y size outline c)) ; A Square is a (make-squar Number Number Number Boolean Symbol) ; interpretation: Supply a good interpretation of Square. (define-struct recta (x y width height outline c)) ; A Rectangle is a (make-recta Number Number Number Number Boolean Symbol) ; interpretation: Supply a good interpretation of Rectangle.
Exercise 1 Add an interpretation for the Square and Rectangle classes. Both represent shapes whose borders are parallel to the borders of a canvas (window).
Exercise 2 Develop the template for functions that consume Shapes.
Exercise 3 Use the template to design shape-shift-x. The function consumes a Shape, sh, and a number, delta. It produces a Shape that is like sh but shifted by delta pixels along the x-axis.
Exercise 4 Use the template to design shape-in?. The function consumes a Shape, sh, and a Posn, p, and returns #true if p is inside or on the boundary of sh.
Domain Knowledge: for a point to be within a circle, its distance to the center must be less than or equal to the circle’s radius. For a point to be within a rectangle, its x coordinate must be between the x coordinate of the left line and the x coordinate of the right line. How do you compute the x coordinates of these lines? Naturally something analogous must hold for the y coordinates. Remember that squares are just special rectangles.
Exercise 5 Use the template to design shape-draw. The function consumes a Shape, sh and an Image, bg and draws sh onto bg.