A directed line segment in the plane can be represented as a pair of vectors -- the vector running from the origin to the start-point of the segment, and the vector running from the origin to the end-point of the segment. Use your vector representation from exercise 2.46 to define a representation for segments with a constructor
make-segment
and selectors start-segment
and end-segment
.As with the representation we produced for vectors in exercise 2.46, segments lend themselves naturally to being represented as pairs. We can put the start of the segment in the first element of the pair, and the end of the segment in the second element of the pair. Then we can access the start and end using
car
and cdr
respectively:
(define (make-segment start-v end-v) (cons start-v end-v)) (define (start-segment seg) (car seg)) (define (end-segment seg) (cdr seg))This then allows us to represent segments and access their components:
> (define segment (make-segment (make-vect 10 5) (make-vect 30 40))) > segment '((10 . 5) 30 . 40) > (start-segment segment) '(10 . 5) > (end-segment segment) '(30 . 40)
No comments:
Post a Comment