問題2.49 – SICP(計算機プログラムの構造と解釈)その63

問題2.49

a. 指定されたフレームの外形を描くペインタ

(define draw-frame-outline
  (let ((v0 (make-vect 0.0 0.0))
        (v1 (make-vect 1.0 0.0))
        (v2 (make-vect 1.0 1.0))
        (v3 (make-vect 0.0 1.0)))
       (segments->painter
         (list (make-segment v0 v1)
               (make-segment v1 v2)
               (make-segment v0 v3)
               (make-segment v3 v2)))))

(draw-frame-outline canvas-frame)
gosh> 0.0,0.0,400.0,0.0
400.0,0.0,400.0,400.0
0.0,0.0,0.0,400.0
0.0,400.0,400.0,400.0
#<undef>

b. フレームの向かい側の頂点を結んで「X」を描くペインタ

(define draw-frame-cross
  (let ((v0 (make-vect 0 0))
        (v1 (make-vect 1 0))
        (v2 (make-vect 1 1))
        (v3 (make-vect 0 1)))
       (segments->painter
         (list (make-segment v0 v2)
               (make-segment v1 v3)))))

(draw-frame-cross canvas-frame)
gosh> 0.0,0.0,400.0,400.0
400.0,0.0,0.0,400.0
#<undef>

c. フレームの辺の中点を結んで菱形を描くペインタ

(define draw-frame-diamond
  (let ((v0 (make-vect 0.5 0.0))
        (v1 (make-vect 1.0 0.5))
        (v2 (make-vect 0.5 1.0))
        (v3 (make-vect 0.0 0.5)))
   (segments->painter
     (list (make-segment v0 v1)
           (make-segment v1 v2)
           (make-segment v2 v3)
           (make-segment v3 v0)))))

(draw-frame-diamond canvas-frame)
gosh> 200.0,0.0,400.0,200.0
400.0,200.0,200.0,400.0
200.0,400.0,0.0,200.0
0.0,200.0,200.0,0.0
#<undef>

d. wave ペインタ

(define wave
  (segments->painter
    (list (make-segment (make-vect 0.35 0.85) (make-vect 0.40 1.00))
          (make-segment (make-vect 0.65 0.85) (make-vect 0.60 1.00))
          (make-segment (make-vect 0.35 0.85) (make-vect 0.40 0.65))
          (make-segment (make-vect 0.65 0.85) (make-vect 0.60 0.65))
          (make-segment (make-vect 0.60 0.65) (make-vect 0.75 0.65))
          (make-segment (make-vect 0.40 0.65) (make-vect 0.30 0.65))
          (make-segment (make-vect 0.75 0.65) (make-vect 1.00 0.35))
          (make-segment (make-vect 0.60 0.45) (make-vect 1.00 0.15))
          (make-segment (make-vect 0.60 0.45) (make-vect 0.75 0.00))
          (make-segment (make-vect 0.50 0.30) (make-vect 0.60 0.00))
          (make-segment (make-vect 0.30 0.65) (make-vect 0.15 0.60))
          (make-segment (make-vect 0.30 0.60) (make-vect 0.15 0.40))
          (make-segment (make-vect 0.15 0.60) (make-vect 0.00 0.85))
          (make-segment (make-vect 0.15 0.40) (make-vect 0.00 0.65))
          (make-segment (make-vect 0.30 0.60) (make-vect 0.35 0.50))
          (make-segment (make-vect 0.35 0.50) (make-vect 0.25 0.00))
          (make-segment (make-vect 0.50 0.30) (make-vect 0.40 0.00)))))

(wave canvas-frame)
gosh> 140.0,340.0,160.0,400.0
260.0,340.0,240.0,400.0
140.0,340.0,160.0,260.0
260.0,340.0,240.0,260.0
240.0,260.0,300.0,260.0
160.0,260.0,120.0,260.0
300.0,260.0,400.0,140.0
240.0,180.0,400.0,60.0
240.0,180.0,300.0,0.0
200.0,120.0,240.0,0.0
120.0,260.0,60.0,240.0
120.0,240.0,60.0,160.0
60.0,240.0,0.0,340.0
60.0,160.0,0.0,260.0
120.0,240.0,140.0,200.0
140.0,200.0,100.0,0.0
200.0,120.0,160.0,0.0
#<undef>

各出力結果を "図形言語 描画スクリプト(with Canvas and JavaScript)" のテキストエリアにコピペして描画させることができます。

計算機プログラムの構造と解釈
ジェラルド・ジェイ サスマン ジュリー サスマン ハロルド エイブルソン
ピアソンエデュケーション
売り上げランキング: 6542
«
»