Review

Review exercise

Working with images

Image dimensions

Colors

Drawing style of basic shapes

Combining images

Exercise

Write a function which takes a size parameter, and two color parameters:

    (define (myShape size color1 color2)
       ...

    (myShape 100 "red" "green")     
    (myShape 200 "red" "green")

and draws shapes as the follows, whose outer size is equal to the given size: images images

Exercise solution:

    (define (myShape size color1 color2)
      (overlay
       (above
        (beside (circle (/ size 4) "solid" color1) (circle (/ size 4) "solid" color1))
        (beside (circle (/ size 4) "solid" color1) (circle (/ size 4) "solid" color1)))
      (square size "solid" color2)))
    (myShape 100 "red" "green")

Case study: checker board

We wish to build a checker board of given size and colors.

In doing so we utilize two helper functions as below:

        ;Function fullchecker: size:Number color1: String color2:String -> Image
        ;Returns an 8 by 8 checker board
        (define (fullchecker size color1 color2)
            (quadruple (quadruple (checker2 size color1 color2))))

        ;Function checker2: size:Number color1: String color2:String -> Image
        ; draws a checker board 2 by 2
        (define (checker2 size color1 color2)
            (above 
                (beside 
                    (square size "solid" color1)
                    (square size "solid" color2))
                (beside 
                    (square size "solid" color2)
                    (square size "solid" color1))))

        ;Function quadruple: image:Image -> Image
        ;returns an image which replicates the input image 2 by 2
        (define (quadruple image)
            (above 
                (beside image image)
                (beside image image)))

        (fullchecker 40 "red" "green")

Coordinate system and finer placement

Scaling, flipping, rotating, and angles

Exercise

Write a function to draw a butterfly of given size and color:

    (define (butterfly size color angle)
      ...

    (butterfly 200 "red" 60)
    (butterfly 200 "red" 80)

which produces an image like follows, whose outer size is equal to the given size:, images images

Exercise solution:

    (define (butterfly size color angle)
      (beside
       (above 
        (rotate 45 (rhombus (/ size 2) angle "solid" color))
        (rotate -45 (rhombus (/ size 2) angle "solid" color)))
       (above 
        (rotate 315 (rhombus (/ size 2) angle "solid" color))
        (rotate -315 (rhombus (/ size 2) angle "solid" color)))))

    (butterfly 200 "red" 60)
    (butterfly 200 "red" 80)

Animations

Animations: creating a frame

The above function is intended to progress a ball downwards over an empty-scene as frame no increases, thus creating an animation of a falling ball.

Animations: starting

The animation is shown on a separate window, and can be stopped by either closing that window or pressing 'stop'.

Exercise

Exercise solution

    (define (animation2 frameNo)
      (place-image 
       (circle 20 "solid" "red")
       frameNo 50
       (place-image 
        (circle 20 "solid" "green")
        50 frameNo  ;note how the y-coordinate is related to frameNo
        (empty-scene 400 400))))
    (animate animation2)



    (define speedX 1.2)
    (define speedY 0.5)
    (define (animation2 frameNo)
      (place-image 
       (circle 20 "solid" "red")
       (* speedX frameNo) 50
       (place-image 
        (circle 20 "solid" "green")
        50 (* speedY frameNo)  ;note how the y-coordinate is related to frameNo
        (empty-scene 400 400))))
    (animate animation2)