Assignment 3: Drawing Procedures


Due: 10:30 p.m. Tuesday 18 September 2012

Summary: In this assignment, you will use the functional drawing paradigm to create interesting images by combining procedures.

Purposes: To practice writing procedures, nesting functional calls, and decomposing problems.

Expected Time: Two to three hours.

Collaboration: We encourage you to work in groups of size three. You may, however, work in a group of size two. You may not work alone. You may discuss this assignment with anyone, provided you credit such discussions when you submit the assignment. When you work in a group, please collaborate on every problem - do not break up the work so that one person works on problem 1, another on problem 2, and another on problem 3.

Submitting: Email your answer to . The title of your email should have the form CSC-151-02 Assignment 3 and should contain your answers to all parts of the assignment. Scheme code should be in the body of the message. You should not attach any images; I should be able to re-create them from your code.

Warning: So that this assignment is a learning experience for everyone, we may spend class time publicly critiquing your work.

Preliminaries

This weeks's theme sounds pretty straightforward: "Put a square pin in a round hole." So it looks something like the following image.

To solve the problem, we'll need to break it down into more manageable pieces.

  • You need to be able to create a square drawing of any color, size, and location.
  • You need to be able to create a circle drawing of any color, size, and location.
  • You need to be able to superimpose drawings, one on top of the other.
  • You'll need to do some geometry to figure out what is the size of the largest square that fits into a circle.
  • You'll need to be able to figure out where to place the square to be sure it's in the center of the circle.

Breaking down problems into manageable pieces is an essential skill the computer scientist needs. Then you can program the manageable pieces. And finally you put the solution together from all the pieces you've solved.

But wait: There's more! Often, in the process of solving a clearly stated problem your imagination will run wild; and you will think of cool and beautiful things that you can do using the pieces you've created to solve the given problem. Then the creative artist in you can come out and express itself. So once you've done all we've specifically asked you to do, the final exercise will give you free rein to produce something beautiful.

Assignment

Problem 1: Non-Unit Shapes

(a) Write a procedure (square-thing col row side-length color) that creates a drawing of a square with the specified side length whose top-left corner is at (col,row) and has the appropriate color.

(b) Write a procedure (round-thing col row diameter color) that creates a drawing of a circle with the specified diameter whose top-left corner is at (col,row) and has the appropriate color.

Problem 2: Pegs in Holes

(a) Write a procedure (round-peg-in-square-hole col row side-length peg-color hole-color), which creates a square of the specified side length and top left corner at (col,row) with a circle that is as large as possible inserted.

(image-show (drawing->image (round-peg-in-square-hole 20 20 50 "red" "black") 100 100))

(b) Next we will want to put a square peg in a round hole, but there are a few challenges to tackle first. The round peg in the square hole was easy because both things (the round-thing and the square-thing) had the same coordinates for their top left corners and the side-length of the square was the same as the diameter of the circle. Neither of these two facts will hold for the square peg in a round hole.

What is the side length of the largest square that will fit inside a circle? To answer this question, you'll need to use the Pythagorean theorem and the lengths indicated by the diagram below.

Once you've figured the answer, write the procedure (largest-square-in-circle diameter) to calculate the side length of the largest square that will fit into a circle of the specified diameter.

Similarly, you will note that for a square in a circle, the top left corners don't match anymore. In fact there is an offset. With a little more geometry, you could confirm this procedure calculates the offset appropriately:

(define largest-square-offset 
  (lambda (diameter) 
    (/ (- diameter
          (largest-square-in-circle diam)) 
        2)))

Note that this procedure depends on yours, so in order for it to be correct, yours will have to be correct also.

(c) Now that you have largest-square-in-circle and largest-square-offset, you can write the procedure (square-peg-in-round-hole col row diameter peg-color hole-color), which takes a circle of the specified diameter and top left corner at (col,row) inserts a square that is as large as possible.

(image-show (drawing->image (square-peg-in-round-hole 20 20 50 "red" "black") 100 100))

Problem 3: Combining procedures

With some (less than?) basic building blocks in place, you can now show off your creativity and imagination. The following procedure puts a round peg in a square hole in a round peg in a square hole.

(define double-round-peg-in-square-hole
  (lambda (col row diameter peg-color hole-color)
    (drawing-join (round-peg-in-square-hole col row diameter peg-color hole-color)
                  (round-peg-in-square-hole
                    (+ col (largest-square-offset diameter))
                    (+ row (largest-square-offset diameter))
                    (largest-square-in-circle diameter)
                    peg-color
                    hole-color))))

(a) Write code to generate two drawings as shown in the following two images.

(b) Use the techniques and procedures above, as well as any others you deem useful or interesting, write a program that generates an interesting image by combining at least four peg-in-hole drawings in any geometry or color arrangement you wish. Here is an example; your imaginations should show there are many other possibile explorations.

Important Evaluation Criteria

We intend to evaluate your assignment on the correctness, elegance, and clarity of your solutions. That is, we will check whether your procedures do what they are intended to do, whether they are reasonably concise, and whether you have chosen a technique that is clear and easy to understand.


Jerod Weinman

Copyright 2007-2012 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.

Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License .