CSC151.02 2010S Functional Problem Solving : Labs

Laboratory: Drawings as Values


Summary: In this laboratory, you will explore the model of images in which images are created by composing and modifying basic drawing objects.

Preparation

a. In your Web browser, open a new window with the reference page for the drawings procedures. (You can reach the page by clicking on the References:Primary and then on the appropriate link)

b. Start GIMP and open the MediaScript console.

c. Do not create a new image.

Exercises

Exercise 1: Basic Explorations

In the following, whenever you are asked to name something, put the definition in the definitions pane. However, when you are asked to show something, put the command in the interactions pane.

a. Starting with the unit circle, create a circle of radius 50 and name that circle d1. Leave the circle black and filled. Put your definition of this circle in the definitions pane.

Hint! Instructions for creating a larger circle are in the reading.

(define d1 (drawing-___ ___ ___))

b. Display your drawing with the following instruction, which you enter in the interactions pane.

> (image-show (drawing->image d1 100 50))

c. Look at the underlying representation of your circle by asking the Scheme interpreter for the value of d1.

> d1

d. Create a new drawing, named d2, by coloring d1 red.

(define d2 (drawing-___ d1 "red"))

e. Display the new drawing to see that you created it successfully.

> (image-show (drawing->image d2 100 50))

f. Create a new drawing, named d3, by shifting d2 right 100 units.

(define d3 (drawing-___ d2 ___))

g. Display the new drawing to see that you created it successfully.

> (image-show (drawing->image d3 100 50))

h. Look at the value of d3. Do your best to explain each of the parts of that value.

> d3

i. Create a new drawing, named d4, by grouping d1 and d3.

(define d4 (drawing-___ d1 d3))

j. Display the new drawing to see that you created it successfully.

> (image-show (drawing->image d4 100 50))

k. Create a new drawing, named d5, by shifting d4 upward 10 units.

(define d5 (drawing-___ d4 ___))

l. Display the new drawing to see that you created it successfully.

> (image-show (drawing->image d5 100 50))

m. Create a new drawing, named d6, by recoloring d5 to yellow.

(define d6 (drawing-___ ___ ___)

n. Display the new drawing to see that you created it successfully.

> (image-show (drawing->image d6 100 50))

o. Create a new drawing, named d7, by grouping d4 and d6.

(define d7 (drawing-___ ___ ___))

p. Display the new drawing to see that you created it successfully.

> (image-show (drawing->image d7 100 50))

q. What do you predict will be the effect on d7 if you replace the definition of d1 by the following?

(define d1 (drawing-scale drawing-unit-square 40))

r. Check your answer experimentally.

> (image-show (drawing->image d7 100 50))

Exercise 2: A Simple Drawing

In the previous exercise, we created a complex drawing step-by-step, defining each intermediate drawing along the way. However, if we're drawing a single shape, we could also nest operations.

a. Write a single definition that creates a blue circle of radius 30, centered at (80,40). Name that circle blue-circle.

(define blue-circle ___)

b. Display the new drawing in a 100x100 image to see that you created it successfully.

> (image-show (drawing->image blue-circle 100 100))

c. What do you expect the underlying representation to look like? Fill in the blanks below.

(drawing ellipse 255 "" ___ ___ ___ ___)

d. Check your answer experimentally.

> blue-circle

Exercise 3: Experiments with Rendering

You've just rendered your blue circle in a 100x100 image. Does image size affect rendering? Let's check that question.

a. What do you expect to have happen if you render blue-circle in a 200x200 image?

b. Check your answer experimentally.

c. What do you expect to have happen if you render the same drawing in a 10x10 image?

d. Check your answer experimentally.

Exercise 4: Scaling Drawings

Consider the following definition.

(define sample-square
 (drawing-recolor
  (drawing-hshift
   (drawing-vshift
    (drawing-scale
     drawing-unit-square
     40)
    100)
   60)
  "green"))

a. In English, describe what shape you think this describes.

b. Check your answer experimentally by displaying the drawing.

c. Where is the left margin of the square? Where is the top margin?

d. Now, suppose we define a new drawing by scaling the square.

(define sample2 (drawing-scale sample-square 1.5))

Describe what you expect the result to look like.

e. Check your answer experimentally.

f. Where is the left margin of the scaled square? Where is the top margin? Were they what you expected? Why do you think they are what they are?

g. Now, suppose we define a new drawing by scaling the square in only one direction.

(define sample3 (drawing-hscale sample2 0.75))

Describe what you expect the result to look like.

h. Check your answer experimentally.

Exercise 5: Order of Operations

As you know, when we're working with mathematics, the order in which we apply operations matters a lot. For example, if we both add two to and square a number, it matters whether we add first or square first. Does order matter in drawings? Let's consider a few examples. (Note: We explore the examples visually. You may also find it useful to see what the underlying representations look like.)

We'll start with a simple black circle along with copies of it colored red and blue.

(define subject (drawing-scale drawing-unit-circle 40))
(define subject-red (drawing-recolor subject "red"))
(define subject-blue (drawing-recolor subject "blue"))

a. Suppose we want to translate the circle horizontally 50 units and vertically 50 units. Does the order we do those two operations matter?

b. Use the following code to check your answer. What do you expect it to do? Does it do what you expect?

(image-show
 (drawing->image
  (drawing-group subject
                 (drawing-hshift (drawing-vshift subject-red 50) 40)
                 (drawing-vshift (drawing-hshift subject-blue 40) 50))
  100 100))

c. Suppose we want to scale the circle horizontally by 1/2 and vertically by 3/2. Does the order we do those two operations matter?

d. Use the following code to check your answer.

(image-show
 (drawing->image
  (drawing-group subject
                 (drawing-hscale (drawing-vscale subject-red 1.5) 0.5)
                 (drawing-vscale (drawing-hscale subject-blue 0.5) 1.5))
  100 100))

e. Suppose we want to translate the circle horizontally by 50 units and scale it by 1/2. Does the order we do those two operations matter?

f. Use the following code to check your answer.

(image-show
 (drawing->image
  (drawing-group subject
                 (drawing-hshift (drawing-scale subject-red 0.5) 40)
                 (drawing-scale (drawing-hshift subject-blue 40) 0.5))
  100 100))

For Those With Extra Time

If you find that you have time left, you may want to do an extra problem, which focuses more on the programming side of things, or an exploration, which focuses a bit more on the design side of things.

Extra 1: Some Compound Drawings

Consider the following definitions.

(define thing (drawing-recolor (drawing-scale drawing-unit-circle 30) "orange"))
(define g1
  (drawing-group thing
                 (drawing-hshift thing 40)
                 (drawing-hshift thing 80)
                 (drawing-hshift thing 120)))
(define g2 
  (drawing-group g1
                 (drawing-recolor (drawing-vshift (drawing-hshift g1 20) 30) 
                                  "green")
                 (drawing-recolor (drawing-vshift (drawing-hshift g1 40) 60) 
                                  "blue")
                 (drawing-recolor (drawing-vshift (drawing-hshift g1 20) 90) 
                                  "indigo")))
(define g3 
  (drawing-group (drawing-recolor g2 "black")
                 (drawing-recolor (drawing-hshift (drawing-vshift g2 5) 5) 
                                  "white")
                 (drawing-hshift (drawing-vshift g2 10) 10)))
(define g4 
  (drawing-group g2
                 (drawing-hscale g2 1.5)
                 (drawing-vscale (drawing-vshift g2 5) 0.75)))

a. Describe what you think each of the four drawings will look like.

b. Check your answers experimentally. Note that some of these drawings take surprisingly long to render.

c. What clever (or not so clever) technique do these examples reveal?

Explorations

Try using the drawing procedures to create a smiley face or an office building.

Creative Commons License

Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright (c) 2007-10 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 license, visit http://creativecommons.org/licenses/by-nc/2.5/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.