Functional Problem Solving (CSC 151 2013F) : Labs
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] [FAQ] [IRC] [Teaching & Learning] [Grading]
Current: [Assignment] [EBoard] [Lab] [Outline] [Partners] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Partners] [Readings]
Reference: [Setup] - [Functions A-Z] [Functions By Topic] - [Racket] [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [Davis (2013F)] [Rebelsky (2010F)] [Weinman (2012F)]
Misc: [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] [Issue Tracker (Course)]
Summary: In this laboratory, you will explore how one writes Scheme programs that instruct the GIMP to create some simple images.
a. Start GIMP and enable the DBUS Server.
b. Then start DrRacket and instruct it to
(require gigls/unsafe). Click
.
If things go wrong, see our instructions for running the CSC 151 software. Ask for help if you need it.
c. At the end of this lab is a list of all the important procedures discussed in the corresponding reading. Quickly review that list.
Here are the instructions for drawing a simple face from the reading.
(define smiley (image-new 200 200))
(image-show smiley)
; Draw the primary circle
(image-select-ellipse! smiley REPLACE
10 10 180 180)
(context-set-fgcolor! "yellow")
(image-fill-selection! smiley)
(context-set-fgcolor! "black")
(context-set-brush! "Circle (09)")
(image-stroke-selection! smiley)
; Draw the eyes
(image-select-ellipse! smiley REPLACE
50 60 30 20)
(image-select-ellipse! smiley ADD
120 60 30 20)
(context-set-fgcolor! "white")
(image-fill-selection! smiley)
(context-set-fgcolor! "black")
(context-set-brush! "Circle Fuzzy (07)")
(image-stroke-selection! smiley)
(image-select-ellipse! smiley REPLACE
60 60 10 20)
(image-select-ellipse! smiley ADD
130 60 10 20)
(context-set-fgcolor! "lightsteelblue")
(image-fill-selection! smiley)
; Smile
(image-select-ellipse! smiley REPLACE
40 60 120 100)
(image-select-ellipse! smiley SUBTRACT
40 45 120 100)
(context-set-fgcolor! "white")
(image-fill-selection! smiley)
(context-set-fgcolor! "red")
(context-set-brush! "Calligraphic Brush#3")
(image-stroke-selection! smiley)
; Get ready to show
(image-select-nothing! smiley)
(context-update-displays!)
a. Copy and paste these instructions into your definitions pane. Read through the instructions to see what image you predict they will draw.
b. Unfortunately, the GIMP's list of brushes has changed since we created
those instructions. Use (context-list-brushes) to see
the names of the available brushes. Then, change any calls to
context-set-brush to use one of those brushes.
c. Click and observe what happens. If all has gone well, you should see a new image with a smiling face.
d. In the interactions pane, write instructions for drawing a nose onto the image. Your instructions might look something like the following:
>(image-select-rectangle! smiley REPLACE 90 90 20 20)>(image-select-ellipse! smiley INTERSECT 80 85 30 30)>(context-set-fgcolor! "green")>(image-fill-selection! smiley)
If the instructions don't seem to change the image, try clicking
on the image window or typing (context-update-displays!).
e. It is possible to clear an image by selecting everything and then clearing the selection. Do so now.
>(image-select-all! smiley)>(image-clear-selection! smiley)>(context-update-displays!)
f. Sometimes it's more fun to see an image drawn little-by-little. Instead of clicking the button, you can copy and paste sections of the code from the definitions pane to the interactions pane.
Try this for the first few instructions in the image. We'd recommend
that you copy sections that end with image-fill-selection!
or image-stroke-selection!. You may have to call
context-update-displays! to see the change.
a. Open a new DrRacket window or tab and write a series of instructions in the definitions pane to draw a face different than the sample we gave above. You should try a different shape of face, different brushes or colors, and different facial features.
b. What do you expect to happen if you change the size of the image in which you're drawing, such as a 100x100 image or a 300x300 image?
c. Check your answer experimentally.
d. If changing the size of the image yielded results that you found undesirable, summarize how you might fix them. (Don't fix them right now, just indicate how you might fix them.)
e. Now, suppose that we define two names, width and
height. Describe how you might change your face
code to take those values into account.
(define width 300) (define height 200) (define canvas (image-new width height)) ...
(If you've learned procedures, you can also consider how to do this using a procedure.)
f. It's not worth the time to update all the parts of your face. Pick a few parts (say, the outline of the face and one eye) and update those parts to take the width and height of the image into account.
Suppose we've defined picname as a string naming an
image.
(define picname "/home/rebelsky/glimmer/samples/rebelsky-stalkernet.jpg")
a. Write a series of instructions to load and show the picture.
b. Write a series of instructions to draw a diagonal line from the
upper-left-hand-corner to the lower-right-hand corner of the image.
Note that you'll need to use image-width
and image-height to determine the width and
height of the image.
c. Write a series of instructions to put some kind of border at the edges of the image. (E.g., select a rectangle ten pixels from each edge and then stroke that rectangle.)
a. Write a series of instructions that create a new image and then draw a house in in the image. (Yes, you are restricted in that you don't know how to make triangles. Deal with it.)
b. Suppose we start by defining two names, width and
height. Describe how you might change your house
code to take those values into account.
(define width 300) (define height 200) (define canvas (image-new width height)) ...
(If you've learned procedures, you can also consider how to do this using a procedure.)
Revise your instructions from Exercise 2 so that the drawing adapts to the size of the image.
Recall that you can obtain the width and height of the image using the
image-width and image-height
procedures.
(image-new
width
height)
(image-load
filename)
(image-show
image)
(image-blot!
image
col
row)
(image-draw-line!
image
col1
row1
col2
row2)
(image-select-ellipse!
image
selection-type
left
top
width
height)
left, top margin is
top, width is width
and height is height.
If selection-type is
REPLACE, the ellipse replaces the
current selection. If selection-type
is ADD, the ellipse is added to the
current selection. If selection-type
is SUBTRACT, the ellipse is subtracted from
the current selection. If selection-type
is INTERSECT, the ellipse is intersected
with the current selection (that is, only points that are in
both the current selection and the ellipse remain selected).
(image-select-rectangle!
image
selection-type
left
top
width
height)
left, top margin is
top, width is width
and height is height.
If selection-type is
REPLACE, the rectangle replaces the
current selection. If selection-type
is ADD, the rectangle is added to the
current selection. If selection-type
is SUBTRACT, the rectangle is subtracted from
the current selection. If selection-type
is INTERSECT, the rectangle is intersected
with the current selection (that is, only points that are in
both the current selection and the rectangle remain selected).
(image-select-all!
image)
(image-select-nothing!
image)
(image-fill-selection!
image)
(image-stroke-selection!
image)
(context-set-brush!
brush-name)
,
(context-set-brush!
brush-name
brush-size)
(context-set-fgcolor!
color)
(context-list-brushes)
(context-list-brushes
pattern)
(context-update-displays!)
(usleep
usec)
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] [FAQ] [IRC] [Teaching & Learning] [Grading]
Current: [Assignment] [EBoard] [Lab] [Outline] [Partners] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Partners] [Readings]
Reference: [Setup] - [Functions A-Z] [Functions By Topic] - [Racket] [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [Davis (2013F)] [Rebelsky (2010F)] [Weinman (2012F)]
Misc: [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] [Issue Tracker (Course)]
Samuel A. Rebelsky, rebelsky@grinnell.edu
Copyright (c) 2007-2013 Janet Davis, Samuel A. Rebelsky, and Jerod Weinman. (Selected materials are copyright by John David Stone or Henry Walker and are used with permission.)

This work is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this
license, visit http://creativecommons.org/licenses/by-nc/3.0/
or send a letter to Creative Commons, 543 Howard Street, 5th Floor,
San Francisco, California, 94105, USA.