;; to draw a Sierpinski triangle down at a, b, and c, ;; assuming it is large enough (define (sierpinski a b c depth) (cond [(= depth 0) true] [else (local ((define a-b (mid-point a b)) (define b-c (mid-point b c)) (define c-a (mid-point a c))) (and (draw-triangle a b c) (sierpinski a a-b c-a (- depth 1)) (sierpinski b a-b b-c (- depth 1)) (sierpinski c c-a b-c (- depth 1))))])) ;; mid-point : posn posn -> posn ;; to compute the mid-point between a-posn and b-posn (define (mid-point a-posn b-posn) (make-posn (mid (posn-x a-posn) (posn-x b-posn)) (mid (posn-y a-posn) (posn-y b-posn)))) ;; mid : number number -> number ;; to compute the average of x and y (define (mid x y) (/ (+ x y) 2)) (define (draw-triangle a b c) (and (draw-solid-line a b 'black) (draw-solid-line b c 'black) (draw-solid-line c a 'black))) ;(define A (make-posn 200 0)) ;(define B (make-posn 27 300)) -173,+300 ;(define C (make-posn 373 300)) +173, 1 2 sqrt(5) (define PI 3.141593) (define top (make-posn 160 10)) (define side 300) (define left (make-posn (- (posn-x top) (/ side 2)) (* (sin (/ PI 3)) side))) (define right (make-posn (+ (posn-x top) (/ side 2)) (* (sin (/ PI 3)) side))) (start 320 300) (sierpinski top left right 10)