;; Quiz 2 sample solution
;;
;; this code requires the draw teachpack.
;;
;; draw-square: posn number color -> true
;;
;; (draw-square position width color)
;; draws a square with the upper left corner determined by
;; the posn structure position using the width and color.
;;
;; sample usage: (draw-square (make-posn 100 100) 50 'Red)
;;
(define (draw-square position width color)
(draw-solid-rect position width width color))
;; =========================================================
;; draw-2squares: posn number color color -> true
;;
;; (draw-2squares position width color1 color2)
;; draw two squares next to each other horizontally
;; the first square is drawn at posn (upper left corner of
;; the first sqare matches the posn. The first square is
;; drawn using color1.
;; The function draws another square immediately to the right
;; of the first square, but using color2
;;
;; sample usage: (draw-2squares (make-posn 100 100) 50 'Red 'Black)
(define (draw-2squares position width color1 color2)
(and
;; draw first square at postion
(draw-square position width color1)
;; draw second sqaure at postition shifted width along x axis
(draw-square (make-posn
(+ width (posn-x position))
(posn-y position))
width color2)))
;; =========================================================
;; draw-2x2: posn number color color -> true
;;
;; (draw-2x2 position width color1 color2)
;; draws 4 squares arranged in a square. The upper left square
;; drawn has it's top left corner at position and color is color1.
;; There is also square drawn immediately to the right of the first
;; sqaure using color 2. Two more squares are drawn, below these two,
;; the colors are reversed to create something like a 2x2 checkerboard.
;;
;; sample usage: (draw-2x2 (make-posn 100 100) 50 'Red 'Black)
(define (draw-2x2 position width color1 color2)
(and
;; draw top 2 squares
(draw-2squares position width color1 color2)
;; draw bottom 2 squares
(draw-2squares (make-posn
(posn-x position)
(+ width (posn-y position)))
width
color2
color1)))
;; =========================================================
;; draw-4x4: posn number color color -> true
;;
;; (draw-4x4 position width color1 color2)
;; draws 16 squares arranged in a square. The upper left square
;; drawn has it's top left corner at position and color is color1.
;; The remaining 15 squares complete a 4x4 checkerboard that extends
;; down and to the right of the first square.
;;
;; sample usage: (draw-4x4 (make-posn 100 100) 50 'Red 'Black)
(define (draw-4x4 position width color1 color2)
(and
(draw-2x2 position width color1 color2)
(draw-2x2 (make-posn
(+ width width (posn-x position))
(posn-y position)) width color1 color2)
(draw-2x2 (make-posn
(posn-x position)
(+ width width (posn-y position))) width color1 color2)
(draw-2x2 (make-posn
(+ width width (posn-x position))
(+ width width (posn-y position))) width color1 color2)
))
;; =========================================================
;; draw-8x8: posn number color color -> true
;;
;; (draw-8x8 position width color1 color2)
;; draws 64 squares arranged in a square. The upper left square
;; drawn has it's top left corner at position and color is color1.
;; The remaining 63 squares complete a 8x8 checkerboard that extends
;; down and to the right of the first square.
;;
;; sample usage: (draw-8x8 (make-posn 100 100) 50 'Red 'Black)
(define (draw-8x8 position width color1 color2)
(and
(draw-4x4 position width color1 color2)
(draw-4x4 (make-posn
(+ (* width 4) (posn-x position))
(posn-y position)) width color1 color2)
(draw-4x4 (make-posn
(posn-x position)
(+ (* width 4) (posn-y position))) width color1 color2)
(draw-4x4 (make-posn
(+ (* width 4) (posn-x position))
(+ (* width 4) (posn-y position))) width color1 color2)
))
;; test code for draw-8x8 (which uses the other functions to draw
;; a checkerboard.
;(start 600 600)
;(draw-8x8 (make-posn 100 100) 50 'Red 'Black)