Summary: In this laboratory, you will explore some of the issues that pertain to writing your own procedures.
a. Review How Scheme Evaluates Expressions (version 2).
b. Make a copy of
procedures-lab.scm, which contains most of the code from the reading.
c. Review the file to see what values and procedure are included. (You may find it easiest to look at the list provided by the Index button in MediaScript.
a. Verify that the four basic values look as they are described in the corresponding reading.
(image-show (drawing->image black-circle 200 100))
(image-show (drawing->image purple-ellipse 200 100))
(image-show (drawing->image blue-i 200 100))
(image-show (drawing->image red-eye 200 100))
b. Try some of the transformers. For example,
(image-show (drawing->image (variant-1 black-circle) 200 100))
(image-show (drawing->image (variant-1 (variant-1 black-circle)) 200 100))
(image-show (drawing->image (add-right-neighbor red-eye) 200 100))
c. Try the
(image-show (drawing->image (circle ___ ___ ___) 200 100))
d. Verify that
correctly squares the
numbers 5, 10, -3, 1.2, and 0.05.
Now that we can draw circles using
, it will be helpful to write
procedures that generate other simple shapes. Let's start with squares.
How do we create a drawing of a square? It depends on how we want to describe the square. If we are centering the square on a particular point, we need to know (1) the x coordinate of the center, (2) the y coordinate of the center, and (3) the edge length of the square. We can then scale the unit square by the edge length and shift it horizontally by the x coordinate of the center and vertically by the y coordinate of the center. Suppose we are drawing a square with edge length 25, centered at (20,30). We might write
(define my-square (drawing-hshift (drawing-vshift (drawing-scale drawing-unit-square 25) 30) 20))
Now, let's think about how to generalize this.
a. Write a procedure,
that creates a drawing of a square. (We couldn't call this procedure
, because we'd already used
that name for the procedure that squares numbers.) You should be
able to generalize the code above, and base your procedure on the
b. However, most of us don't like to draw our squares centered on a particular point. We'd rather specify the left edge and the top edge. How can we do that? Suppose we want to draw a square of side-length 30, with the left edge at 17 and the top edge at 42. We first scale the unit square by 30. That means that the left edge, which was at -1/2, is now at -15 (that is, -1/2 * 30). The top edge, similarly, is also at -15. To get the left edge at 17, we need to shift it horizontally by 32 (that is, 15 + 17). To get the top edge at 42, we need to shift it vertically by 59 (that is, 15 + 42).
In Scheme, we might write
(define another-square (drawing-hshift (drawing-vshift (drawing-scale drawing-unit-square 30) (+ (/ 30 2) 42)) (+ (/ 30 2) 17)))
Of course, we can also generalize this code.
Write a procedure
( that creates a drawing of a square of the
specified edge length, with the left side of the square at
left and the top side at
One deficiency of the
from the reading and of the
drawing-of-square procedures you've just reading
is that they don't allow you to specify the color of
the circle or square.
Rewrite the three procedures to take a color as a parameter. For example,
(circle 20 10 10 "blue")
Write a procedure,
that creates a drawing of a rectangle. You should do your
best to figure out appropriate parameters. Here is a sample call, using
the parameters we find most natural.
(rectangle 20 10 50 80 "yellow")
If you'd like to know what we thought were appropriate parameters, you can look at the notes on this exercise.
For each of the procedures above, you've likely tested your procedure by applying it to some drawing, rendering that drawing to some image, and then showing the image. For example,
(image-show (drawing->image (rectangle 20 10 50 80) 200 100))
Write a procedure,
( that renders the drawing on
a 200x100 image and then shows the image. Once we've written
that procedure, we can more easily check drawings, as in the
(check-drawing (rectangle 20 10 50 80))
As you may recall, the
procedure makes a copy of a drawing, places the copy immediately
to the right of the original drawing, and combines the two into a
a. Write a procedure,
( that combines a drawing with
a duplicate neighbor that is 75% of the size of the original drawing.
As you write this procedure, you will find it useful to examine the
b. Write a procedure,
that combines a drawing with a duplicate neighbor of the same size,
but that overlaps 20%
(so that the left end of the neighbor is 20% left of the right end
of the original).
c. Write a procedure,
that combines a drawing with a duplicate neighbor immediately to
the right, with the neighbor the same height, but half the width
of the original.
d. Write a procedure,
that builds a duplicate neighbor that is immediately below the drawing.
e. Write a procedure,
( that builds a 25% larger neighbor that is immediately below the drawing.
We now have procedures that pair an image with a smaller right neighbor and a larger bottom neighbor. What happens if we combine these transformations? For each of the following, predict what the image will look like and then render it to check your prediction.
(add-larger-bottom-neighbor (add-smaller-right-neighbor red-eye))
(add-smaller-right-neighbor (add-larger-bottom-neighbor red-eye))
(add-smaller-right-neighbor (add-nearer-right-neighbor red-eye))
(add-smaller-right-neighbor (add-smaller-right-neighbor red-eye))
(add-narrow-right-neighbor (add-narrow-right-neighbor red-eye))
(add-smaller-right-neighbor (add-narrow-right-neighbor red-eye))
(add-narrow-right-neighbor (add-smaller-right-neighbor red-eye))
If you have extra time, you may find it useful to do any of the following exercises. (You need not do them in order.) You may also choose to do one of the explorations.
In the reading, we defined a drawing that looks a bit like a red eye.
Write your own procedure,
builds a drawing of an eye, based on the values of its parameters.
What parameters should it have? Certainly, the color of the iris.
However, you might find other parameters useful, too.
As you may recall, one potentially confusing aspect of the
procedure is that it not
only scales the drawing, it also scales the distance of the drawing
from the top-left corner.
Write a procedure,
(, that scales a drawing, but
does not move the drawing. That is, the scaled drawing has the
same left edge and the same top edge as the original drawing.
If you're not sure how to approach this problem, you may want to read the notes on this exercise.
a. Write a procedure,
( that creates a compound
drawing with four copies of drawing (one shifted right, one
shifted down, and one shifted down and right).
b. Write a procedure that takes the result of
two-by-two and scales it by 50%, so that
the resulting drawing is the same width and height as the original.
c. Using this new procedure, build a four-by-four grid of one of the original images, or one of your own choosing.
For each of these explorations, you may find the following drawing an appropriate starting point. You might also start with a house or other drawing you've designed. You might even start with a simple shape.
(define sample (drawing-hshift (drawing-vshift (drawing-group (drawing-scale drawing-unit-circle 100) (drawing-scale (drawing-recolor drawing-unit-circle "yellow") 90) (drawing-vshift (drawing-hscale (drawing-vscale drawing-unit-circle 40) 60) 10) (drawing-hscale (drawing-vscale (drawing-recolor drawing-unit-circle "yellow") 40) 60) (drawing-vshift (drawing-group (drawing-hshift (drawing-group (drawing-hscale (drawing-vscale (drawing-recolor drawing-unit-circle "white") 15) 30) (drawing-hscale (drawing-vscale (drawing-recolor drawing-unit-circle "blue") 15) 15)) 18) (drawing-hshift (drawing-group (drawing-hscale (drawing-vscale (drawing-recolor drawing-unit-circle "white") 15) 30) (drawing-hscale (drawing-vscale (drawing-recolor drawing-unit-circle "green") 15) 15)) -18)) -15)) 50) 50))
In Exercise 5, each of the expressions takes one drawing and turn it into four drawings. Explore other ways to use this kind of replication to build an interesting image. (You might also think about ways to create eight, sixteen, or even more copies of the image.)
In the Exercise 4, you developed a number of procedures that pair an image with a neighbor. Create a few variants of those procedures that change the neighbor in an “interesting” way. For example, you might scale it differently horizontally and vertically, you might have it overlap the original figure, you might shift it both horizontally and vertically.
We would recommend that your procedure have the form
(define rectangle (lambda (left top width height color) ..._))
The alternate scaling algorithm is fairly straightforward:
Copyright (c) 2007-9 Janet Davis, Matthew Kluber, Samuel A. Rebelsky, and Jerod Weinman. (Selected materials copyright by John David Stone and Henry Walker and used by permission.)
This material is based upon work partially supported by the National Science Foundation under Grant No. CCLI-0633090. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.
This work is licensed under a Creative Commons
Attribution-NonCommercial 2.5 License. To view a copy of this
or send a letter to Creative Commons, 543 Howard Street, 5th Floor,
San Francisco, California, 94105, USA.