Functional Problem Solving (CSC 151 2013F) : Assignments

Assignment 5: Producing Playful Polygons


Due: 10:30 p.m., Tuesday, 8 October 2013

Summary: In this assignment, you will use turtles to explore mechanisms for constructing images based on a variety of polygons. Our focus will be on using lists, iteration, and anonymous procedures as ways to work with the turtle model.

Purposes: To give you more experience with the turtle model. To give you more comfort with anonymous procedures. To explore the complexities possible from simple operations.

Collaboration: If you have been assigned to a group, you must work with your assigned partners on this assignment. The partner assignments are available at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC151/2013F/partners/assignment.05.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/151hw5pro 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/151hw5epi 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 5 - Playful Polygons and should contain your answers to all parts of the assignment. Scheme code should be in the body of the message.

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

Assignment

Problem 1: Drawing Polygons

Write a procedure, (turtle-polygon! turtle side-length sides), that uses a turtle to draw a regular polygon with the specified number of sides, with each side of the specified length.

Important: Your procedure must return the turtle to its original position and angle. Your procedure must not change the turtle's brush or color.

Hint: Use repeat. The turtle must turn a total of 360 degrees to return to its original angle.

For example,

(turtle-polygon! t 100 3)

(turtle-polygon! t 100 4)

(turtle-polygon! t 60 5)

(turtle-polygon! t 40 6)

Problem 2: Spinning Polygons

Write a procedure, (turtle-spin-polygon! turtle side-length sides angle copies), that draws the given number of copies of the specified polygon by calling your turtle-polygon! procedure, with the turtle turned an angle of angle between polygons.

Important: As in problem one, your procedure must return the turtle to its original position and orientation.

For example,

(turtle-spin-polygon! t 50 4 15 10)

(turtle-spin-polygon! t 50 4 20 5)

(turtle-spin-polygon! t 50 4 5 20)

(turtle-spin-polygon! t 50 4 -30 5)

Problem 3: Scaling Polygons

Write a procedure, (turtle-scale-polygon! turtle initial-side-length sides scale-factor copies), that draws the given number of copies of the specified polygon, with each copy drawn with a side length scale-factor times the the previous side length.

For example, if the initial side length is ten, and the scale factor is two, this procedure would draw polygons with side lengths 10, 20, 40, 80, 160, ....

Similarly, if the initial side length is 100, and the scale factor is 0.9, the procedure would draw polygons with side lengths 100, 90, 81, 72.9, ....

Important: As in the previous problems, your procedure must return the turtle to its original position and orientation.

Hint: The expt function will be useful for finding the ratio of each polygon's side length to the original side length. For example, for scale factor two, the ratios would be 1, 2, 4, 8, 16, 32, and so on.

Here are some visual issues.

(turtle-scale-polygon! t 1 5 2 8)

(turtle-scale-polygon! t 1 5 1.2 30)

(turtle-scale-polygon! t 100 5 0.9 20)

Problem 4: A New Way to Draw Polygons

A potential deficiency of each of the prior two procedures is that multiple polygons are joined at a vertex. The problem is, of course, that our basic polygon procedure draws polygons starting at a particular point, rather than centered at a certain point.

Copy the following procedure to your definitions pane.

(define turtle-centered-polygon! 
  (lambda (turtle radius sides)
    (let ([interior-angle (/ (* 180 (- sides 2)) sides)])
      (turtle-up! turtle)
      (turtle-forward! turtle radius)
      (turtle-down! turtle)
      (turtle-turn! turtle (- 180 (/ interior-angle 2)))
      (turtle-polygon! turtle (* 2 radius (sin (/ pi sides))) sides)
      (turtle-turn! turtle (/ interior-angle 2))
      (turtle-up! turtle)
      (turtle-forward! turtle radius)
      (turtle-turn! turtle 180)
      (turtle-down! turtle))))    

Experiment with this procedure to learn what it does. (Hint: Note that this procedure assumes your turtle-polygon! draws the first side of the polygon before turning.)

a. Document this procedure using the six-P style of documentation.

b. Explain why each expression in the procedure's body is necessary to achieve the result that you see. That is, explain each step of the algorithm in English.

Hint: You may find it useful to comment out each expression using a semicolon, one at a time, to see how the result of leaving out that expression differs from the desired result.)

Problem 5: Spun and Scaled Polygons, Revisited

Write two procedures, (turtle-spin-centered-polygon! turtle radius sides angle copies) and (turtle-scale-centered-polygon! turtle initial-radius sides scale-factor copies).

These procedures should be very similar to turtle-spin-polygon! and turtle-scale-polygon! from problems 2 and 3, except that they will call the turtle-centered-polygon! procedure from Problem 4 instead of your turtle-polygon! procedure from Problem 1.

Problem 6: Spun and Scaled Polygons, Re-Revisited

Write a procedure, (turtle-spin-and-scale-centered-polygon! turtle initial-radius sides angle scale-factor copies), that makes the given number of copies of the polygon, scaling and spinning each subsequent polygon. Again, call the turtle-centered-polygon! procedure from Problem 4.

Problem 7: Creative Polygons

Write a program that systematically generates an interesting image composed of a series of polygons. In addition to spinning and scaling polygons, you should incorporate at least one additional element, such as changing the polygons' positions, changing colors or brushes, or even changing the number of sides between different polygons.

Important Evaluation Criteria

We will judge your solutions on their correctness, their conciseness, and the cleverness. We will also judge your solution to problem 7 on its creativity.


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.