;; The first three lines of this file were inserted by DrScheme. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-beginner-reader.ss" "lang")((modname draw_shapes) (read-case-sensitive #t) (teachpacks ((lib "testing.ss" "teachpack" "htdp") (lib "guess.ss" "teachpack" "htdp") (lib "master.ss" "teachpack" "htdp") (lib "draw.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "testing.ss" "teachpack" "htdp") (lib "guess.ss" "teachpack" "htdp") (lib "master.ss" "teachpack" "htdp") (lib "draw.ss" "teachpack" "htdp"))))) ;; The structure represents a circle ;; center is the position (posn structure) of the center ;; radius is its radius (a non-negative number), and ;; color is a symbol representing a color (define-struct circle (center radius color)) ;; draw-a-circle: circle -> true ;; the function draws a circle structure as a disk ;; and returns true (define (draw-a-circle a-circle) (draw-solid-disk (circle-center a-circle) (circle-radius a-circle) (circle-color a-circle)) ) ;; clear-a-circle: circle -> true ;; the function clears a disk corresponding to a circle ;; and returns true (define (clear-a-circle a-circle) (clear-solid-disk (circle-center a-circle) (circle-radius a-circle)) ) (define (move-circle a-circle x-shift y-shift) (make-circle (make-posn (+ (posn-x (circle-center a-circle)) x-shift) (+ (posn-y (circle-center a-circle)) y-shift)) (circle-radius a-circle) (circle-color a-circle) ) ) ;; The structure represents a rectangle ;; left-upper is the position (posn structure) of its ;; left upper corner, height and width are its height and ;; width (non-negative numbers), and ;; color is a symbol representing a color (define-struct rectangle (left-upper height width color)) ;; draw-a-rectangle: rectangle -> true ;; the function draws a rectangle structure as a solid rectangle ;; and returns true (define (draw-a-rectangle a-rectangle) (draw-solid-rect (rectangle-left-upper a-rectangle) (rectangle-width a-rectangle) (rectangle-height a-rectangle) (rectangle-color a-rectangle)) ) (define (clear-a-rectangle a-rectangle) (clear-solid-rect (rectangle-left-upper a-rectangle) (rectangle-width a-rectangle) (rectangle-height a-rectangle)) ) (define (move-rectangle a-rect x-shift y-shift) (make-rectangle (make-posn (+ (posn-x (rectangle-left-upper a-rect)) x-shift) (+ (posn-y (rectangle-left-upper a-rect)) y-shift)) (rectangle-height a-rect) (rectangle-width a-rect) (rectangle-color a-rect) )) (define shapes (cons (make-circle (make-posn 200 200) 100 'red) (cons (make-rectangle (make-posn 150 220) 20 100 'yellow) (cons (make-rectangle (make-posn 144 150) 20 20 'black) (cons (make-rectangle (make-posn 225 150) 20 20 'black) empty))))) (start 400 400) ;; draw-shapes: a list of shapes -> boolean ;; the function takes a list of shapes (circles, rectangles) ;; and draws them on the canvas in order (define (draw-shapes the-shapes) (cond [(empty? the-shapes) true] [(circle? (first the-shapes)) (and (draw-a-circle (first the-shapes)) (draw-shapes (rest the-shapes)))] [(rectangle? (first the-shapes)) (and (draw-a-rectangle (first the-shapes)) (draw-shapes (rest the-shapes)))] [else false] ) ) (draw-shapes shapes) (sleep-for-a-while 1) (define (clear-shapes the-shapes) (cond [(empty? the-shapes) true] [(circle? (first the-shapes)) (and (clear-a-circle (first the-shapes)) (clear-shapes (rest the-shapes)))] [(rectangle? (first the-shapes)) (and (clear-a-rectangle (first the-shapes)) (clear-shapes (rest the-shapes)))] [else false] ) ) (clear-shapes shapes) (define (move-shapes the-shapes x-shift y-shift) (cond [(empty? the-shapes) empty] [(circle? (first the-shapes)) (cons (move-circle (first the-shapes) x-shift y-shift) (move-shapes (rest the-shapes) x-shift y-shift))] [(rectangle? (first the-shapes)) (cons (move-rectangle (first the-shapes) x-shift y-shift) (move-shapes (rest the-shapes) x-shift y-shift))] ) ) ;(draw-shapes (move-shapes shapes 100 -100)) (define rand1 (- (random 200) 100)) (define rand2 (- (random 200) 100)) (draw-shapes (move-shapes shapes rand1 rand2)) (sleep-for-a-while 1) (clear-shapes (move-shapes shapes rand1 rand2)) (draw-shapes (move-shapes shapes (- (random 200) 100) (- (random 200) 100)))