sicp 2.49

Exercise 2.49.  Use segments->painter to define the following primitive painters:

a.  The painter that draws the outline of the designated frame.

b.  The painter that draws an ``X'' by connecting opposite corners of the frame.

c.  The painter that draws a diamond shape by connecting the midpoints of the sides of the frame.

d.  The wave painter.

 

(define (outline-painter)
  (let ((outline-segments
         (list
          (make-segment
           (make-vect 0.0 0.0)
           (make-vect 0.0 1.0))
          (make-segment
           (make-vect 0.0 1.0)
           (make-vect 1.0 1.0))
          (make-segment
           (make-vect 1.0 1.0)
           (make-vect 1.0 0.0))
          (make-segment
           (make-vect 1.0 0.0)
           (make-vect 0.0 0.0)))))
    (segments->painter outline-segments)))

(define (x-painter)
  (let ((x-segments
         (list
          (make-segment
           (make-vect 0.0 0.0)
           (make-vect 1.0 1.0))
          (make-segment
           (make-vect 0.0 1.0)
           (make-vect 1.0 0.0)))))
    (segments->painter x-segments)))

(define (diamond-painter)
  (let ((diamond-segments
         (list
          (make-segment
           (make-vect 0.5 0.0)
           (make-vect 0.0 0.5))
          (make-segment
           (make-vect 0.0 0.5)
           (make-vect 0.5 1.0))
          (make-segment
           (make-vect 0.5 1.0)
           (make-vect 1.0 0.5))
          (make-segment
           (make-vect 1.0 0.5)
           (make-vect 0.5 0.0)))))
    (segments->painter diamond-segments)))

(define (wave-painter)
  (let ((wave-segments
         (list
          ;part 1
          (make-segment
           (make-vect 0.0 0.83)
           (make-vect 0.16 0.6))
          (make-segment
           (make-vect 0.16 0.6)
           (make-vect 0.31 0.65))
          (make-segment
           (make-vect 0.31 0.65)
           (make-vect 0.41 0.65))
          (make-segment
           (make-vect 0.41 0.65)
           (make-vect 0.35 0.85))
          (make-segment
           (make-vect 0.35 0.85)
           (make-vect 0.41 1.0))
          ;part 2
          (make-segment
           (make-vect 0.6 1.0)
           (make-vect 0.66 0.85))
          (make-segment
           (make-vect 0.66 0.85)
           (make-vect 0.6 0.65))
          (make-segment
           (make-vect 0.6 0.65)
           (make-vect 0.75 0.65))
          (make-segment
           (make-vect 0.75 0.65)
           (make-vect 1.0 0.36))
          ;part 3
          (make-segment
           (make-vect 0.0 0.65)
           (make-vect 0.15 0.4))
          (make-segment
           (make-vect 0.15 0.4)
           (make-vect 0.3 0.6))
          (make-segment
           (make-vect 0.3 0.6)
           (make-vect 0.64 0.5))
          (make-segment
           (make-vect 0.64 0.5)
           (make-vect 0.25 0.0))
          ;part 4
          (make-segment
           (make-vect 0.4 0.0)
           (make-vect 0.5 0.29))
          (make-segment
           (make-vect 0.5 0.29)
           (make-vect 0.4 0.0))
          ;part 5
          (make-segment
           (make-vect 0.75 0.0)
           (make-vect 0.61 0.56))
          (make-segment
           (make-vect 0.61 0.56)
           (make-vect 1.0 0.15)))))
    (segments->painter wave-segments)))
 

你可能感兴趣的:(SICP)