Exam 1: Image Representations and Scheme Basics


Assigned: Friday, 6 February 2009

Due: Beginning of class, Wednesday, 11 February 2009

Preliminaries

Exam format

This is a take-home examination. You may use any time or times you deem appropriate to complete the exam, provided you return it to me by the due date.

There are 10 problems, each worth 10 points for a total of 100 points. Although each problem is worth the same amount, problems are not necessarily of equal difficulty.

Read the entire exam before you begin.

I expect that someone who has mastered the material and works at a moderate rate should have little trouble completing the exam in a reasonable amount of time. In particular, this exam is likely to take you about two to three hours, depending on how well you've learned the topics and how fast you work. You should not work more than four hours on this exam. Stop at four hours and write “There's more to life than CS” and you will earn at least 75 points on this exam.

I would also appreciate it if you would write down the amount of time each problem takes. Each person who does so will earn two points of extra credit. Since I worry about the amount of time my exams take, I will give two points of extra credit to the first two people who honestly report that they've spent at least three hours on the exam or completed the exam. (At that point, I may then change the exam.)

Academic honesty

This examination is open book, open notes, open mind, open computer, open Web. However, it is closed person. That means you should not talk to other people about the exam. Other than as restricted by that limitation, you should feel free to use all reasonable resources available to you.

As always, you are expected to turn in your own work. If you find ideas in a book or on the Web, be sure to cite them appropriately. If you use code that you wrote for a previous lab or homework, cite that lab or homework and the other members of your group.

Although you may use the Web for this exam, you may not post your answers to this examination on the Web. And, in case it's not clear, you may not ask others (in person, via email, via IM, by posting a please help message, or in any other way) to put answers on the Web.

Because different students may be taking the exam at different times, you are not permitted to discuss the exam with anyone until after I have returned it. If you must say something about the exam, you are allowed to say “This is among the hardest exams I have ever taken. If you don't start it early, you will have no chance of finishing the exam.” You may also summarize these policies. You may not tell other students which problems you've finished. You may not tell other students how long you've spent on the exam.

You must include both of the following statements on the cover sheet of the examination.

  1. I have neither received nor given inappropriate assistance on this examination.
  2. I am not aware of any other students who have given or received inappropriate assistance on this examination.

Please sign and date each statement. Note that the statements must be true; if you are unable to sign either statement, please talk to me at your earliest convenience. You need not reveal the particulars of the dishonesty, simply that it happened. Note also that inappropriate assistance is assistance from (or to) anyone other than Professor Weinman (that's me) or Professor Davis.

Presenting Your Work

You must present your exam to me in two forms: both physically and electronically. That is, you must write all of your answers using the computer, print them out, number the pages, put your name on the top of every page, and hand me the printed copy. You must also email me a copy of your exam. You should create the emailed version by copying the various parts of your exam and pasting them into an email message. In both cases, you should put your answers in the same order as the problems. Failure to name and number the printed pages will lead to a penalty of two points. Failure to turn in both versions may lead to a much worse penalty.

In many problems, I ask you to write code. Unless I specify otherwise in a problem, you should write working code and include examples that show that you've tested the code. Do not include images; I should be able to regenerate those.

Unless I explicitly ask you to document your procedures, you need not write introductory comments.

Just as you should be careful and precise when you write code and documentation, so should you be careful and precise when you write prose. Please check your spelling and grammar. Since I should be equally careful, the whole class will receive one point of extra credit for each error in spelling or grammar you identify on this exam. I will limit that form of extra credit to five points.

I will give partial credit for partially correct answers. I am best able to give such partial credit if you include a clear set of work that shows how you derived your answer. You ensure the best possible grade for yourself by clearly indicating what part of your answer is work and what part is your final answer.

Getting Help

I may not be available at the time you take the exam. If you feel that a question is badly worded or impossible to answer, note the problem you have observed and attempt to reword the question in such a way that it is answerable. Feel free to try to call me in the office (269-9812) as well.

I will also reserve time at the start of classes next week to discuss any general questions you have on the exam.

Problems

Part A: Scripting the GIMP Tools

Problem 1: No!

Topics: Procedures, GIMP tools, generalization.

The following code draws a circle with a slash through it, as on “No Smoking” signs, on an image called canvas.

  (context-set-fgcolor! "red")
  (context-set-brush! "Circle (05)")
  (image-select-ellipse! canvas REPLACE 5 5 90 90)
  (image-stroke! canvas)
  (image-draw-line! canvas 0 0 100 100)
  (image-select-nothing! canvas)
  (context-update-displays!)

a. Define a procedure called image-draw-no-sign! that generalizes the code as much as possible. That is, you must imagine what things someone who calls your procedure might want to vary or customize.

b. Write a short statement explaining what parameters your procedure has and why. If appropriate, explain any parameters you considered but chose not to use.

Part B: Drawings as Values

Problem 2: Drawing a Flag, Take One

Topics: Drawings as values, code reading, debugging, paradigms.

The following code is intended to draw the flag of Switzerland, a white cross on a red field. (See Flags of the World - Switzerland.)

This is not actually what it does. Fix it.

(define field (drawing-scale drawing-unit-square 30))
(drawing-recolor field "red")
(define cross  
  (drawing-group
   (drawing-vscale (drawing-hscale drawing-unit-square 20) 6)
   (drawing-vscale (drawing-hscale drawing-unit-square 6) 20)))
(drawing-recolor cross "white")
(define flag (drawing-group field cross))
(drawing-vshift flag 15)
(drawing-hshift flag 15)
(image-show (drawing->image flag 30 30))

Problem 3: Drawing a Flag, Take Two

Topics: Drawings as values, conciseness.

Now that you've drawn the flag of Switzerland, consider the flag of Mauritius: four horizontal strips of equal size, colored red, blue, yellow, and green. (See Flags of the World - Mauritius.) Write a program (that is, a sequence of expressions) that uses drawings-as-values to draw the flag of Mauritius. Make your code as concise as you can.

Problem 4: Drawing Grids

Topics: Drawings as values, procedures.

Write a procedure, (two-by-two drawing) that creates a compound drawing with four copies of drawing (one shifted right, one shifted down, and one shifted down and right). This new, compound drawing should be scaled by 50%, so that it is the same width and height as the original.

Using two-by-two, build a four-by-four grid of some interesting drawing, such as the Swiss flag from Problem 2.

Part C: Image Processing

Problem 5: Image Thresholding

Topics: Numeric values, writing procedures.

Images are often represented, not in colors, but simply as brightness values. Casually called black-and-white, these images are usually referred to as "grayscale," because each pixel contains a single number representing the brightness on a "gray scale" from black to white. Like color components, brightness values are represented on a scale from 0 to 255.

A common operation is to threshold such an image to convert it into a "binary" representation that contains only two values, black and white (represented by zero and 255, respectively), so that any brightness at or above the threshold becomes white, and any value below becomes black.

Write a procedure (brightness-threshold gray-value thresh) that takes a brightness gray-value and a corresponding threshold thresh, returning 0 if gray-value is less than thresh, and 255 otherwise.

Problem 6: Documentation

Write the 6-P documentation for your brightness-threshold procedure.

Problem 7: Color Averaging

Topics: RGB colors, numeric values, writing procedures.

Write a procedure (rgb-average color1 color2) that takes two RGB colors as parameters and computes the average of these two colors. That is, the procedure should produce a new RGB color where the red component is the average of the red components of the two colors, the green component is the average of the green components of the two colors, and the blue component is the average of the blue components of the two colors.

Part D: Turtles and Triangles

Topics: Turtle graphics, numbers, procedures.

In this part, you will be building the tools needed to draw an isosceles triangle using turtle graphics. You will demonstrate your ability to use numeric procedures and define new procedures. We also hope you will learn something about breaking a complex algorithm down into smaller parts that can be solved independently---what computer scientists call functional decomposition.

Note that we wrote these procedures in the opposite order: We started to write turtle-isosceles-triangle!, and then realized we needed more procedures to help us. You can start writing your solutions to these three problems in any order. At some point, you may find yourself working on all three together.

Problem 8: Converting Angles

As you may have noticed, turtles understand angles in degrees, while Scheme's trigonometric functions assume angles are measured in radians. Implement a procedure, degrees->radians, that takes as its parameter an angle measured in degrees and returns an equivalent angle measured in radians. Recall that 360 degrees is equivalent to 2 pi radians. (Hint: How will you test your procedure to ensure it's correct?)

Problem 9: Isosceles Triangle Geometry

An isosceles triangle has two sides of the same length, and therefore also two angles that are the same. Given the width of the base and the apex angle at which the two equal sides meet, we can determine the length of the two remaining sides and also the two remaining angles.

(a) Implement the procedure (isosceles-base-angle apex-angle), which takes as a parameter the apex angle measured in degrees, and computes the remaining two angles of the triangle as follows: base-angle = (180 - apex-angle) / 2

(b) Implement the procedure (isosceles-leg-length base-width apex-angle), which takes as parameters the width of the base and the apex angle, and computes the length of the remaining two legs of the triangle as follows: leg-length = base-width / (2 * sine(apex-angle / 2))

Problem 10: Drawing an Isosceles Triangle

Finally, write a procedure, (turtle-isosceles-triangle! turtle base-width apex-angle), that draws an isosceles triangle with the given base width and apex angle. As with most turtle drawing procedures, you should assume that the user has already placed the turtle where they want to start drawing. The turtle should start by drawing the base of the triangle.

Hint: Your procedure will be much simpler if it makes use of the mathematical procedures described in the previous two problems. Even if you were not able to solve the previous two problems, try writing code that would use those procedures if they were correct.

For example, the code below should produce an image similar to the image below.

    (define canvas (image-new 200 200))
    (define terry (turtle-new canvas))
    (image-show canvas)
    (turtle-teleport! terry 100 100)
    (turtle-isosceles-triangle! terry 50 20)
    (turtle-isosceles-triangle! terry 50 90)
    (turtle-turn! terry 90)
    (turtle-forward! terry 20)
    (turtle-isosceles-triangle! terry 30 120)

Some questions and answers

Here we will post answers to questions of general interest. Please check here before emailing your questions!

Problem 2

Question: Do we have to modify the given code for this problem, or can we write our own code from scratch to draw the flag of Switzerland?
Answer: You should modify the given code. The point of the problem is to demonstrate your understanding of drawings-as-values.

Problem 4

Question: When I use two-by-two on a small drawing, things seem off-center. What's going on?
Answer: When you scale a drawing down by half, if its size is odd, then the resulting drawing's size will be rounded. Thus, some things many not look quite right. Chances are good it's not a bug in your procedure, but a result of this fact. If you want to verify this, you could try doubling the size of your drawing before passing it to two-by-two.

Problem 4

Question: What size should the 4x4 grid be?
Answer: Like the 2x2 grid, it should be the same size as the original image.

Errata

Here you will find errors of spelling, grammar, and design that students have noted. Remember, each error found corresponds to a point of extra credit for everyone. We usually limit such extra credit to five points. However, if we make an astoundingly large number of errors, then we will provide more extra credit.

  • The phrase “each pixel is contains” should read “each pixel contains”. [NEC, 1 point]

Jerod Weinman