Functional Problem Solving (CSC 151 2015S) : Assignments
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [FAQ] [Teaching & Learning] [Grading] [Taking Notes] [Rubric] - [Calendar]
Current: [Assignment] [EBoard am] [EBoard pm] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards am] [EBoards pm] [Labs] [Outlines] [Readings] - [Examples] [Handouts]
Reference: [Setup] [VM] [Errors] - [Functions A-Z] [Functions By Topic] - [Racket] [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [Davis (2013F)] [Rebelsky (2014F)] [Weinman (2014F)]
Misc: [Submit Questions] - [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] - [Book Office Hours] - [Issue Tracker (Course)]
Due: 10:30 p.m. 7 April 2015
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: You may work alone or in groups of two or three. You may discuss the assignment with anyone, provided you cite such collaboration.
Submitting:
Email your answer to <grader-151@cs.grinnell.edu>. The title of your email
should have the form CSC 151.00 Assignment 7: Producing Playful Polygons and
should contain your answers to all parts of the assignment. Scheme code
should be in the body of the message.
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/151hw7pro 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/151hw7epi 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.
Warning: So that this assignment is a learning experience for everyone, we may spend class time publicly critiquing your work.
Write a procedure, (, that uses a turtle to draw a
regular polygon with the specified number of sides, with each side of
the specified length.
turtle-polygon!
turtle side-length
sides)
Important: Your procedure must use the turtle's
existing position and orientation when it starts, returning the
turtle to its original position and orientation when it completes.
Your procedure must not change the turtle's
brush or color. Note that to return the turtle to its position and
orientation, you must figure out how the position and orientation
have changed. You may not use
turtle-teleport! or
turtle-face! (in part because you don't
have an easy way to tell what direction it was facing or where
it was on the canvas).
Hint: Use repeat. The
turtle must turn a total of 360 degrees to return to its original
angle.
In the following examples, you can assume that the turtle was initially moved to the top-left corner of each polygon.
(turtle-polygon! t 100 3)
(turtle-polygon! t 100 4)
(turtle-polygon! t 60 5)
(turtle-polygon! t 40 6)
Write a procedure, (, that draws the given number
of copies of the specified polygon by calling your
turtle-spin-polygon!
turtle side-length
sides angle
copies)turtle-polygon! procedure,
with the turtle turned an angle
of angle between polygons.
Important: As in problem one, your procedure should start the turtle from its current position and orientation, returning the turtle to its original position and orientation when the procedure is complete.
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)
Write a procedure, (, that draws the given number
of copies of the specified polygon, with each copy drawn with a
side length turtle-scale-polygon!
turtle initial-side-length
sides scale-factor
copies)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 start from and 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)
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 6Ps.
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.)
Write the procedure
(.
turtle-spin-centered-polygon!
turtle radius
sides angle
copies)
The procedure turtle-spin-centered-polygon!
should spin the polygon around a center point, as opposed to the
vertex point as in turtle-spin-polygon!.
This procedure should be very similar to
turtle-spin-polygon!
from problem 2, except that it will call
the turtle-centered-polygon!
procedure from Problem 4 instead of your
turtle-polygon! procedure from Problem 1.
Your first polygon procedure draws the outline of a polygon. What if, instead of drawing a simple polygon, we wanted to position the turtle at each of the vertices of a polygon as preparation for some other action? If that other action is provided in the form of a procedure that takes a turtle as a parameter and leaves the turtle in the same position it started in, the task is similar to our original polygon creating procedure.
Write a procedure (,
that applies the procedure turtle-action-polygon!
turtle side-length
sides action!)action! to
turtle at the vertex of a polygon with the
specified number of sides, with each side of the specified length.
You may assume that the action! procedure
returns the turtle to the same angle and position.
Important: As in the previous problems, your procedure must return the turtle to its original position and orientation.
(turtle-action-polygon! t 65 5 (lambda (turtle) (turtle-spin-polygon! turtle 20 4 15 5)))
(turtle-action-polygon! t 8 50 (lambda (turtle) (turtle-polygon! turtle 5 20 )))
(turtle-action-polygon! t ...)
(a) Write a program that systematically generates an interesting image composed of a series of polygons. You should purposefully choose a background color and the color(s) of the polygons to produce some effect on the viewer.
You may call the procedures you wrote in problems 1-6 with carefully chosen parameters to produce a complex image, or you may write and document new procedures (such as one that both spins and scales progressively). In addition to colors, you may also wish to choose particular brushes.In addition to scaling and spinning polygons, you might think about varying the sizes and positions of the polygons in other ways, or even changing the number of sides between different polygons.
(b) Write a short paragraph explaining what color scheme you chose and why, drawing on appropriate terminology about design and color.
We will judge your solutions on their correctness, their conciseness, and their cleverness. We will also judge your solution to problem 7 on its creativity and on the clarity of your intentions.