Due: 1/9, midnight. Language: class/0.
For this exercise, you will develop a structure-based representation of circles and functions that operate on circles, and then develop a class-based representation of circles.
A circle has a radius and color. They also have a position, which is given by the coordinates of the center of the circle (using the graphics coordinates system).
The circ structure and functions.
Design a structure-based data representation for Circle values.
Design the functions =?, area, move-to, move-by, stretch, draw-on, to-image, within?, overlap?, and change-color.
Here are a few examples to give you some ideas of how the functions should work (note you don’t necessarily need to use the same structure design as used here).First, let’s define a few circles we can use:
A (make-circ R C X Y) is interpreted as a circle of radius R, color C, and centered at position (X,Y) in graphics-coordinates.The to-image function turns a circle into an image:
> (to-image c1) > (to-image c2) > (to-image c3)While the draw-on function draws a circle onto a given scene: > (draw-on c1 (empty-scene 200 200)) > (draw-on c2 (empty-scene 200 200)) > (draw-on c3 (empty-scene 200 200)) > (draw-on c1 (draw-on c2 (draw-on c3 (empty-scene 200 200))))The area function computes the area of a circle: > (area c1)
> (area c2)
> (area c3)
314.1592653589793The move-to function moves a circle to be centered at the given coordinates:
> (draw-on (move-to c1 100 100) (empty-scene 200 200))While move-by moves a circle by the given change in coordinates: > (draw-on (move-by c1 -30 20) (empty-scene 200 200))The within? function tells us whether a given position is located within the circle; this includes any points on the edge of the circle:The change-color function produces a circle of the given color: > (to-image (change-color c1 "purple"))The =? function compares two circle for equality; two rectangles are equal if they have the same radius and center point— we ignore color for the purpose of equality: > (=? c1 c2)
> (=? c1 c1)
> (=? c1 (change-color c1 "purple"))
#tThe stretch function scales a circle by a given factor:
> (draw-on (stretch c1 3/2) (empty-scene 200 200))The overlap? function determines if two circles overlap at all: > (overlap? c1 c2)
> (overlap? c2 c1)
> (overlap? c1 c3)
The circ% class.
Develop a class-based data representation for Circle values. Develop the methods corresponding to all the functions above.
The methods should work similar to their functional counterparts: