Introduces additional operations on images. As students use these operations to create more interesting images, they can practice function composition, fitting contracts together, and writing nested expressions.
Learn how to use advanced image operations
Practice function composition
Practice using contracts to help with composing operations
Practice writing and evaluating nested expressions
Learn how to import gif, png, and other images from files
Students create scaled, rotated, flipped, and layered images
Editing environment (WeScheme or DrRacket with the bootstrap-teachpack installed)
Computer for each student (or pair), running WeScheme or DrRacket (If using DrRacket, make sure the Images.rkt file is loaded)
Student Workbooks, and something to write with
Earlier, you learned how to create simple images using operators such as circle, rectangle, and triangle. We can combine or manipulate these basic shapes to make more interesting ones, the same way we can combine and manipulate numbers. In this lesson, you’ll learn Racket functions for manipulating and combining images.
Use of the board is critical in this activity - you’ll want to have lots of room to write, and lots of visuals for students to see. Have students review some of the Image-producing functions they already know (triangle, circle, etc.). Quiz them on the contracts for these functions.
Imagine that we wanted to make an image of a simple satellite that looks like the one shown here. This image contains a blue circle and a red rectangle, with the circle on top of the rectangle. Racket has a function called overlay, which lets you put one image on top of another. Here is its contract, and a purpose statement that explains what it does:
Start out by reminding students why contracts matter: they specify types instead of values, which makes them really flexible! You can demonstrate this by showing them the code for a simple image, and then replacing the size of the triangle with a sub-expression: This sets students up to see overlay as a logical extension - instead of image-producing Circles of Evaluation with number-producing subexpressions, there can be image-producing Circles with image-producing subexpressions.
Using overlay, we could make a picture of a satellite. Take a look at the code below, then hit "enter" and see what shape it makes! Can you change the color of the circle? The size of the rectangle? Can you use overlay to put a star on top of both the star and the rectangle? See an example.
Before students type in the code and try it out, ask the class what they think will happen - what will the size be? The color? The text?
This satellite is flying level in the sky. What if a strong wind were blowing, causing the satellite to fly slightly on its side, like the image seen here? Then, we would want the Racket rotate function: Try copying and pasting this code into the editor, and see what shape you get. What happens if you change the number 30?
Have the class convert this code into a Circle of Evaluation.
Let’s look at this code, viewed as a Circle of Evaluation. Our rotate function is shown here, in the blue circle. 30 is the number of degrees we’ll be rotating, and the second input is the Image we want to rotate. That image is the result of overlaying the circle and the rectangle, shown here in red. By looking at this Circle of Evaluation, can you guess the contract for the rotate function?
Can students write the code or draw the Circle of Evaluation for rotating a difference shape by a different amount? Try using a subexpression like (* 2 75) for the rotation, instead of a simple number.
Here are the contract and purpose for rotate:
When it’s time to introduce the new functions, start out by showing them the contract and then an example, as it does in the student guide. Make sure to ask lots of "how do you know?" questions during the code, to remind them that the contract has all the necessary information.
Suppose you wanted to make the satellite bigger, by scaling it up to 2x or 3x it’s original size. Racket has a function that will do just that, called scale. Here is the contract and purpose statement for scale: Below is some code that will scale a star to make it one-half the original size. What would you change to make it bigger instead of smaller? What would you need to change to scale a different-color star? What if you wanted to scale a circle instead? Can you figure out how to scale the entire spaceship?
There are also functions for flipping an image horizontally or vertically, and for scaling images so they get bigger or smaller. Here are contracts and purpose statements for those functions:
After a few of these, try mixing it up! Show students the Racket code or Circle of Evaluation for some of the new functions first, and have them guess the contract based on how they is used.