Due: Tuesday, Oct 5, end of your lab session

Lab2: Have a Nice Day!

This lab will give you practice writing functions in the Racket language and working in the DrRacket programming environment. Furthermore, you will gain further experience with the image.ss teachpack we used in Lab1.

Please try to complete all of Part 1 and as much as you can of Part 2 by the end of your lab session. You are encouraged, but not required to complete Part 2. This week's homework will include Part 2 of this lab. Please hand in all your work at the end of your lab session.


Preliminaries

Start DrRacket.

Set the language level to Beginning Student. (How?)

Add the image.ss teachpack. (How?)

Recall from lab1, that the image.ss teachpack provides functions for creating images. We have used this teachpack to draw circles, such as

(circle 100 'solid 'yellow)

You can find documentation on the available functions by searching DrRacket's help system for "image.ss". We suggest you find the image.ss documentation and leave it open on your screen for the duration of the lab.

DrRacket understands HTML's named colors. These names can be used as symbols with the functions of the image.ss teachpack. You can explore them here.

(circle 20 'solid 'mediumspringgreen)

You may also define your own color, by specifying the amount of primary Red, Green and Blue (RGB) colors to be mixed on a scale of 0 to 255. You can explore generating RGB colors here.

(define myblue (make-color 100 100 255))
(circle 100 'solid myblue)


Part 1

The image.ss package provides functions such as rectangle, circle, ellipse, etc. Let's look up the ellipse function in the image.ss documentation. The function contract
(ellipse w h m c) -> image
specifies that this function takes 4 arguments. The first two are positive numbers specifying the width and height of the ellipse. The third argument specifies the filling mode ('solid or 'outline), while the last argument specifies the drawing color. For example:

(ellipse 100 50 'outline (make-color 0 163 82))

Furthermore, as we also practiced in lab1, images can be put on top of one another by using overlay and overlay/xy. Look up the documentation for these functions.

Write the function

;; face: num -> image
to produce an image similar to the following:
The number consumed by the function face is the radius of the image produced.

Be careful and attempt to match the proportions of the example closely. Your face must not exactly match the face above, but the main part of the face must be a circle and the eyes must be ellipses.

No matter what radius is given, the image of the face should never be distorted; that is, its proportions must remain true at any size. Note how all three of the following faces are similarly proportioned:

These faces are the results of applying face to 40, 80, and 120 respectively.

Hint: Assume we want to draw images of a circle circumscribed by a square. We want this program to be parametrized by the length of the square side. We can easily draw a square of side l, by using l as both the width and height argument for the rectangle function in image.ss. This image of a square would scale by being called with different values for l. But how to keep the circle inscribed in the square? We must make sure its diameter is equal to the square side. This implies that its radius needs to be half the value of l.

;; Contract: circleinsquare: number ->image
;; Purpose: To draw the image of a circle circumscribed in a square of side l
;; Examples (circlesquare 50) should produce a square of side 50 circumscribing a circle

(define (circleinsquare l)
   (overlay
     (rectangle l l 'outline 'blue)
     (circle (/ l 2) 'outline 'red)))

> (circleinsquare 50)

Test this code with different values for l and note the scaling. By defining both figures in terms of parameter l, we have ensured their relative scaling.


Part 2

[Note: For receving check+ credit, you must start working on part 2 of the lab. You are not required to finish by the end of the lab session. You will have a chance to finish part 2 in this week's homework.]

Cheer up!
Write a second function

;; happy-face: num -> image
and leave the original face function intact. Make sure that happy-face, like face, scales too.

Hint: In my creation of the smile, I overlayed circles of different radii to obtain a ring and used the image.ss function shrink-bl to elicit a smile :).


How to hand in your work.

Submit your work according to the submission instructions.


Material designed by Adam Shaw.