2011-09-19

SICP Exercise 2.2: Points and Segments

Consider the problem of representing line segments in a plane. Each segment is represented as a pair of points: a starting point and an ending point. Define a constructor make-segment and selectors start-segment and end-segment that define the representation of segments in terms of points. Furthermore, a point can be represented as a pair of numbers: the x coordinate and the y coordinate. Accordingly, specify a constructor make-point and selectors x-point and y-point that define this representation. Finally, using your selectors and constructors, define a procedure midpoint-segment that takes a line segment as argument and returns its midpoint (the point whose coordinates are the average of the coordinates of the endpoints). To try your procedures, you'll need a way to print points:
(define (print-point p)
  (newline)
  (display "(")
  (display (x-point p))
  (display ",")
  (display (y-point p))
  (display ")"))
Well, we've just been introduced to pairs, so it seems reasonable to use those to represent segments and points. A segment can be a pair of points, and a point can be a pair of coordinates. In each case we're going to have two selectors that will use car and cdr to retrieve the components of the data structure. Here's my definitions:
(define (make-segment p1 p2) (cons p1 p2))
(define (start-segment s) (car s))
(define (end-segment s) (cdr s))

(define (make-point x y) (cons x y))
(define (x-point p) (car p))
(define (y-point p) (cdr p))
Next we're asked to define midpoint-segment. The midpoint of a segment is a point with its x coordinate equal to the average of the x coordinates of the start and end points, and its y coordinate equal to the average of the y coordinates of the start and end points. So our midpoint-segment is going to have to use the segment point selectors to access the start and end points of the segment, and the point x and y selectors to access the components of those points. I'm going to make use of the average procedure that was defined way back in section 1.1.7 to help produce this procedure:
(define (midpoint-segment s)
  (make-point (average (x-point (start-segment s)) (x-point (end-segment s)))
              (average (y-point (start-segment s)) (y-point (end-segment s)))))
Finally, let's see it all in action:
> (print-point (midpoint-segment (make-segment (make-point 0 0) (make-point 4 4))))
(2,2)
> (print-point (midpoint-segment (make-segment (make-point 1 2) (make-point 5 4))))
(3,3)
> (print-point (midpoint-segment (make-segment (make-point 4 -2) (make-point -3 8))))
(1/2,3)

No comments:

Post a Comment