; (define-struct posn (x y)) ; Included from draw.ss. ; ; A posn is: ; - (make-posn num num) ; ; Examples: (define origin (make-posn 0 0)) (define p34 (make-posn 3 4)) ; A color is one of {'red, 'yellow, 'white, 'black, 'blue, 'green}. ; ; Examples of color: (explicitly enumerated in data def'n). ;; translate: posn, num, num --> posn ;; Return the point which is a-point offset by (+x, +y). ;; (define (translate a-point x y) (make-posn (+ (posn-x a-point) x) (+ (posn-y a-point) y))) (translate p34 5 12) = (make-posn 8 16) (translate origin 3 4) = p34 (translate p34 -3 -4) = origin (define-struct circle (location radius color)) ; ; A circle is: ; (make-circle posn num symbol color) ; ; Examples of circles: ; (define circle1 (make-circle p34 1 'red)) ; A little red dot. (define circle23 (make-circle origin 23 'blue)) (define circ1 circle1) (define circ23 circle23) (define-struct rectangle (location width height color)) ;A rectangle is: ; (make-rectangle posn num num color) ; ;Examples: (define rectangle1 (make-rectangle (make-posn 10 10) 15 30 'yellow)) (define rectangle2 (make-rectangle (make-posn 0 0) 2 2 'red)) (define rect1 rectangle1) (define rect2 rectangle2) ;; A shape is a structure that contains ;; - a circle, or ;; - a rectangle #| ;; Examples of shapes: ;; circle1 circle23 rectangle1 rectangle2 |# #| ;; Template: ;; (define (shape-func a-shape) (cond [(circle? a-shape) ..(circle-location a-shape).. ..(circle-radius a-shape).. ..(circle-color a-shape)..] [(rectangle? a-shape) ..(rectangle-location a-shape).. ..(rectangle-width a-shape).. ..(rectangle-height a-shape).. ..(rectangle-color a-shape)..])) |# ;; translate-shape: Shape Num Num --> Shape ;; Return a shape which is like a-shape, but moved by (+x,+y). ;; (define (translate-shape a-shape x y) (cond [(circle? a-shape) (make-circle (translate (circle-location a-shape) x y) (circle-radius a-shape) (circle-color a-shape))] [(rectangle? a-shape) (make-rectangle (translate (rectangle-location a-shape) x y) (rectangle-width a-shape) (rectangle-height a-shape) (rectangle-color a-shape))])) #| ;; Tests: (translate-shape (make-circle (make-posn 0 0) 4 'blue) 2 3) = (make-circle (make-posn 2 3) 4 'blue) (translate-shape (make-rectangle (make-posn 3 7) 4 4 'red) 0 1) = (make-rectangle (make-posn 3 8) 4 4 'red) |# ;(start 500 500) ; Opens a canvas to draw on. ;; draw-shape: Shape --> true ;; Draw a-shape on the current canvas, and return true. ;; (define (draw-shape a-shape) (cond [(circle? a-shape) (draw-solid-disk (circle-location a-shape) (circle-radius a-shape) (circle-color a-shape))] [(rectangle? a-shape) (draw-solid-rect (rectangle-location a-shape) (rectangle-width a-shape) (rectangle-height a-shape) (rectangle-color a-shape))])) #| ;Test-cases (draw circle1) = true (draw circle2) = true (draw rectangle1) = true (draw rectangle2) = true |# ; A constant, used for width of various bands, in part (b). ; (define band-width 20) ;; v.103 : A color is: one of { RED, YELLOW, WHITE, BLACK, BLUE, GREEN}. ;; v.103.5: A color is: one of {'red, 'yellow, 'white, 'black, 'blue, 'green}. (define (rectangle-tool i) (make-rectangle (make-posn (* i band-width) 0) band-width (* i i) 'yellow)) ;(rectangle-tool 0) = (make-rectangle (make-posn 0 0) 20 0 'yellow) ;(rectangle-tool 7) = (make-rectangle (make-posn 140 0) 20 49 'yellow) ;; circle-tool: number -> true ;; Return a circle centered at canvas position (150,150), ;; with radius (* i band-width), ;; and color red if i is odd or blue if i is even. ;; (define (circle-tool i) (make-circle (make-posn 150 150) (* i band-width) (cond [(odd? i) 'red] [else 'blue]))) (circle-tool 6) = (make-circle (make-posn 150 150) 120 'blue) (circle-tool 3) = (make-circle (make-posn 150 150) 60 'red) #| ;; make-shapes: NatNum -> list of shapes ;; Returns a list of shapes created by rectangle-tool, ;; called with n, n-1, n-2, ... 1, 0. ;; ;; Based on the template for natural numbers ;; |# (define (make-shapes n) (cond [(zero? n) (cons (rectangle-tool 0) empty)] [(positive? n) (cons (rectangle-tool n) (make-shapes (sub1 n)))])) #| (make-shapes 0)= (cons (make-rectangle (make-posn 0 0) 20 0 'yellow) empty) (make-shapes 3)= (cons (make-rectangle (make-posn 60 0) 20 9 'yellow) (cons (make-rectangle (make-posn 40 0) 20 4 'yellow) (cons (make-rectangle (make-posn 20 0) 20 1 'yellow) (cons (make-rectangle (make-posn 0 0) 20 0 'yellow) empty)))) |# ;; draw-shapes: list-of-shapes --> true ;; Draw each element of los. ;; (define (draw-shapes los) (cond [(empty? los) true] [(cons? los) (and (draw-shape (first los)) (draw-shapes (rest los)))])) ;(start 500 500) ; ;(draw-list-of-shapes (make-shapes 11)) = true