Functional Problem Solving (CSC 151 2013F) : Assignments

Assignment 3: Drawing Procedures


Due: 10:30 p.m., Tuesday, 17 September 2013

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.

Collaboration: You must work with assigned partners on this assignment. The partner assignments are available at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC151/2013F/partners/assignment.03.txt. You may discuss this assignment with anyone, provided you credit such discussions when you submit the assignment.

Wrapper (Prologue): Individually read through this assignment and make sure that you understand what is required. Then use the form available at http://bit.ly/151hw3pro to indicate (a) how long you think this assignment will take and (b) what you think will be the most challenging aspect of this assignment.

Wrapper (Epilogue): When you are done with the assignment, fill out the form available at http://bit.ly/151hw3epi to indicate (a) how long the assignment took, (b) what the most challenging part of the assignment was, and (c) something important you learned from doing the assignment. If you find that the assignment took much less or much more time than you expected, also include (d) a note as to what might have led to that difference.

Submitting: Email your answer to . The title of your email should have the form CSC 151.02 Assignment 3: Drawing Procedures 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 week you will explore drawings that use the “drawings as values” approach to make two kinds of drawings: one representational, one non-representational.

Representational Drawings: Smiley Faces

Consider the following Scheme code.

(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))

Believe it or not, but this code draws a smiley face.

Can you tell what part of the code generates what part of the face?

While the code sucessfully draws a smiley face, it is not very customizable - it draws only one size of smiley face, at only one location, and in only one way. Wouldn't it be much nicer if we could customize the face?

Non-representational Drawings: Putting a Square Peg in a Round Hole

This weeks's second theme may sound a bit more straightforward: “Put a square peg in a round hole.” That is, we start with something like the following image:

How do we achieve this 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 need to do some geometry to figure out what is the size of the largest square that fits into a circle.
  • You 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 computer scientists need. Once the pieces are small enough, you can implement them. And then you put the solution together from all the individual 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, or at least compelling.

Assignment

Problem 1: Generalized Smiley Faces

As we noted, the smiley code builds only one appearance of smiley face, with a fixed size and location. Wouldn't it be nice to be able to do more? Let's generalize that code!

Write a procedure, smiley, based on the code given above, that takes some inputs and draws a smiley face based on those inputs. Minimally, your procedure should have parameters that represent the the width, height, and center of the smiley face. But you may consider other possible inputs: eye color or shape, mouth color or shape, perhaps the size or relative position of some of the features.

Think carefully about naming the parameters you choose. Write 6Ps documentation so that we can understand how to use your procedure.

Problem 2: 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 3: 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 4: 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-group 
      (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. You may find it useful to write other helper procedures, such as one that creates a round peg in a square hole centered at a particular location.

(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 imagination 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.


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.)

Creative Commons License

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.