;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; U. C. Berkeley ;; ;; EECS Computer Science Division ;; ;; CS3 Lecture 23 Answers ;; ;; (Recursion Potpourri) ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; RANDOMIZED-SIERPINSKI-TRIANGLE ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define (randomized-sierpinski-triangle n) (clear-graphics) (rst-helper n -200 -200)) (define (rst-helper n x y) (draw-point x y) (if (zero? n) 'wow (let ((vert (random 3))) (cond ((= 0 vert) (rst-helper (- n 1) (/ (+ x -200) 2)(/ (+ y -200) 2))) ((= 1 vert) (rst-helper (- n 1) (/ (+ x 200) 2) (/ (+ y -200) 2))) ((= 2 vert) (rst-helper (- n 1) (/ (+ x 0) 2) (/ (+ y 200) 2))))))) (randomized-sierpinski-triangle 100) ;;;;;;;;;;;;;;;;;;; ;; FIBONACCI SERIES ;;;;;;;;;;;;;;;;;;; ;; 1, 1, 2, 3, 5, 8, 13, 21, ... (define (fib n) (if (<= n 2) 1 (+ (fib (- n 1)) (fib (- n 2))))) (fib 6) ;; ==> 8 ;;;;;;;;;;;;;;;;;;;;; ;; FRACTAL -- BIGTREE ;;;;;;;;;;;;;;;;;;;;; ;; Window borders (define *L* -200) (define *R* 200) (define *D* -200) (define *U* 200) ;; DRAW-LINE (define (draw-line x1 y1 x2 y2) (position-pen x1 y1) (draw-line-to x2 y2)) ;; HOUSE (define (house BLx BLy BRx BRy TRx TRy Cx Cy TLx TLy level) (draw-line BLx BLy TLx TLy) (draw-line BRx BRy TRx TRy) (if (= level 0) (begin (draw-line-to Cx Cy) (draw-line-to TLx TLy)))) ;; DRAW-BIGTREE (define (draw-bigtree BLx BLy BRx BRy level) (let* ((scale .5) (xhv (* scale (- BLy BRy))) (yhv (* scale (- BRx BLx))) (TLx (+ BLx xhv)) (TLy (+ BLy yhv)) (TRx (+ BRx xhv)) (TRy (+ BRy yhv)) (Cx (/ (- (+ TRx TLx TLy) TRy) 2)) (Cy (/ (- (+ TRx TLy TRy) TLx) 2))) (if (= level 0) (house BLx BLy BRx BRy TRx TRy Cx Cy TLx TLy level) (begin (house BLx BLy BRx BRy TRx TRy Cx Cy TLx TLy level) (draw-bigtree TLx TLy Cx Cy (- level 1)) (draw-bigtree Cx Cy TRx TRy (- level 1)))))) ;; DEMO-BIGTREE (define (demo-bigtree) (demo-bigtree-helper 0 7)) (define (demo-bigtree-helper n n-finish) (if (> n n-finish) 'done (begin (display `(Hit enter to display frame ,n of ,n-finish)) (read) (clear-graphics) (draw-bigtree (/ *L* 4) *D* (/ *R* 4) *D* n) (demo-bigtree-helper (+ 1 n) n-finish)))) ;; (clear-graphics) ;; (draw-bigtree (/ *L* 4) *D* (/ *R* 4) *D* 7) ;; (demo-bigtree)