;; 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)) ) ;; move-circle: circle number number -> circle ;; the function creates a new circle from a given one ;; with coordinates shifted by x-shift and y-shift (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-height a-rectangle) (rectangle-width a-rectangle) (rectangle-color a-rectangle)) ) ;; clear-a-rectangle: rectangle -> true ;; the function clears a rectangle structure ;; and returns true (define (clear-a-rectangle a-rectangle) (clear-solid-rect (rectangle-left-upper a-rectangle) (rectangle-height a-rectangle) (rectangle-width a-rectangle) ) ) ;; move a rectangle: rectangle number number -> rectangle ;; the function creates a new rectangle from a given one ;; with coordinates shifted by x-shift and y-shift (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) )) ;; a list of shapes (a face) (define face (cons (make-circle (make-posn 200 200) 100 'red) (cons (make-rectangle (make-posn 150 220) 100 20 '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 alosh) (cond [(empty? alosh) true] [(circle? (first alosh)) (and (draw-a-circle (first alosh)) (draw-shapes (rest alosh)))] [(rectangle? (first alosh)) (and (draw-a-rectangle (first alosh)) (draw-shapes (rest alosh)))] ) ) (draw-shapes face) (sleep-for-a-while 2) ;; clear-shapes: a list of shapes -> boolean ;; the function takes a list of shapes (circles, rectangles) ;; and clears them on the canvas in order (define (clear-shapes alosh) (cond [(empty? alosh) true] [(circle? (first alosh)) (and (clear-a-circle (first alosh)) (clear-shapes (rest alosh)))] [(rectangle? (first alosh)) (and (clear-a-rectangle (first alosh)) (clear-shapes (rest alosh)))] ) ) (clear-shapes face) (sleep-for-a-while 1) ;; move-shapes: a list of shapes number number -> a list of shapes ;; the function creates a list of shapes (circles, rectangles) ;; from the given list by shifting the coordinates ;; by x-shift and y-shift (define (move-shapes alosh x-shift y-shift) (cond [(empty? alosh) empty] [(circle? (first alosh)) (cons (move-circle (first alosh) x-shift y-shift) (move-shapes (rest alosh) x-shift y-shift))] [(rectangle? (first alosh)) (cons (move-rectangle (first alosh) x-shift y-shift) (move-shapes (rest alosh) x-shift y-shift))] )) ;(draw-shapes (move-shapes face 100 -100)) ;(sleep-for-a-while 1) ;; multi-move-shapes: a list of shapes, a list of numbers, a list of numbers -> ;; a list of lists of shapes ;; given a list of shapes and a list of x coordinate shifts and a list of y ;; coordinate shifts, creates a list of lists of shapes: for each pair x-shift, ;; y-shift it creates a shift of the original list of shapes by the ;; x-shift in x coordinate and y-shift in y-coordinate ;; If the length of alox is not the same as the length of aloy ;; then the shorter of the two is used. (define (multi-move-shapes alosh alox aloy) (cond [(or (empty? alox) (empty? aloy)) empty] [else (cons (move-shapes alosh (first alox) (first aloy)) (multi-move-shapes alosh (rest alox) (rest aloy)))] ) ) ;; the results of moving the face four times (define faces (multi-move-shapes face (list 50 50 20 -100) (list -50 100 30 70))) ;; draw a list of lists of shapes (define (draw-and-clear-all-shapes alolsh delay) (cond [(empty? alolsh) true] [else (and (draw-shapes (first alolsh)) (sleep-for-a-while delay) (clear-shapes (first alolsh)) (draw-and-clear-all-shapes (rest alolsh) delay))] ) ) (draw-and-clear-all-shapes faces 2) ;; to-do: ;; 1. Write a function to generate a list of coordinate shifts randomly ;; 2. use the randomly generated coordinates for face movements. You might want ;; to use variables to store the canvas width and height to figure out ;; a good range of random numbers. ;; 3. Create another list of shapes (a car, a house, etc...) ;; 4. Write a function to generate a directional movement (a car driving, ;; a house... well... flying up to the sky?...) ;; 5. Alternatively you can write a function that instead of moving a shape ;; will resize it by a certain percentage and use this function to grow or ;; shrink a combined shape (a face, a house, ...) (stop) ;(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)))