On this page:
1.1 Circles
5.92

1 1/9: Circles

Due: 1/9, midnight. Language: class/0.

1.1 Circles

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).

  1. 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:
    > (define c1 (make-circ 25 "red" 100 70))
    > (define c2 (make-circ 50 "blue" 90 30))
    > (define c3 (make-circ 10 "green" 50 80))

    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)

    image

    > (to-image c2)

    image

    > (to-image c3)

    image

    While the draw-on function draws a circle onto a given scene:
    > (draw-on c1 (empty-scene 200 200))

    image

    > (draw-on c2 (empty-scene 200 200))

    image

    > (draw-on c3 (empty-scene 200 200))

    image

    > (draw-on c1 (draw-on c2 (draw-on c3 (empty-scene 200 200))))

    image

    The area function computes the area of a circle:
    > (area c1)

    1963.4954084936207

    > (area c2)

    7853.981633974483

    > (area c3)

    314.1592653589793

    The move-to function moves a circle to be centered at the given coordinates:
    > (draw-on (move-to c1 100 100) (empty-scene 200 200))

    image

    While move-by moves a circle by the given change in coordinates:
    > (draw-on (move-by c1 -30 20) (empty-scene 200 200))

    image

    The within? function tells us whether a given position is located within the circle; this includes any points on the edge of the circle:
    > (within? c1 (make-posn 0 0))

    #f

    > (within? c1 (make-posn 110 80))

    #t

    The change-color function produces a circle of the given color:
    > (to-image (change-color c1 "purple"))

    image

    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)

    #f

    > (=? c1 c1)

    #t

    > (=? c1 (change-color c1 "purple"))

    #t

    The stretch function scales a circle by a given factor:
    > (draw-on (stretch c1 3/2) (empty-scene 200 200))

    image

    The overlap? function determines if two circles overlap at all:
    > (overlap? c1 c2)

    #t

    > (overlap? c2 c1)

    #t

    > (overlap? c1 c3)

    #f

  2. 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:

    > (define c1 (new circ%  25 "red" 100 70))
    > (define c2 (new circ% 50 "blue" 90 30))
    > (send c1 area)

    1963.4954084936207

    > (send c1 draw-on (empty-scene 200 200))

    image