Homework 2: Producing Playful Polygons

Assigned
Thursday January 28
Due
11 p.m. Wednesday February 3
Summary
In this assignment, you will write several functions that use turtles to draw interesting figures. You will also practice using for loops for iteration.
Collaboration
For this assignment, you will work with an assigned partner.
Submission
Online turnin form, Wrapper
Warning
So that this exercise is a learning assignment for everyone, I may spend class time publicly critiquing your work.

Warm up: Problem 0

Write a program named termial.py that does the following:

Here are some example inputs and outputs for you to start your testing with:

Input
1
2
3
4
5
Output
 1
 3
 6
10
15

Aside: "Termial" is not a typo; it is a technical term from number theory.

Assignment

Implement the following functions in a single file named polygons.py.

Requirements

Problem 1: Drawing polygons

Write a void function, draw_polygon(my_turtle, n_sides, side_length), which makes a turtle draw a regular polygon.

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: You may have done this problem under a different name as part of the Chapter 4 exercises.

Examples: If we run the following programs, we will see the following images.

wn = turtle.Screen()
tess = turtle.Turtle()
draw_polygon(tess, 4, 50)
wn.mainloop()
Square of side length 50 drawn with turtle
wn = turtle.Screen()
tess = turtle.Turtle()
draw_polygon(tess, 7, 50)
wn.mainloop()
Septuagon of side length 50 drawn with turtle
wn = turtle.Screen()
tess = turtle.Turtle()
draw_polygon(tess, 8, 50)
wn.mainloop()
Octagon of side length 50 drawn with turtle

Problem 2: Spinning polygons

Write a void function, spin_polygons(my_turtle, n_sides, side_length, angle, n_copies), that draws the given number of copies of the specified polygon by calling your draw_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.

spin_polygons(tess, 4, 50, 30, 5)
Spin 5 copies of a size 50 square with angle 30
spin_polygons(tess, 4, 50, -30, 3)
Spin 3 copies of a size 50 square with angle -30
spin_polygons(tess, 4, 50, 10, 360//10)
Spin 36 copies of a size 50 square with angle 10

Problem 3: Scaling polygons

Write a void function, scale_polygons(my_turtle, n_sides, initial_side_length, scale_factor, n_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 exponentiation operator ** 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.

scale_polygons(tess, 5, 10, 2, 4)
Scale 4 copies of a size 10 square with scale factor 2
scale_polygons(tess, 5, 1, 1.2, 25)
Scale 25 copies of a size 1 square with scale factor 1.2
scale_polygons(tess, 5, 100, 0.9, 20)
Scale 20 copies of a size 100 square with scale factor 0.9

Problem 4: Centered polygons

Consider the following function definition:

import math
def draw_centered_polygon(my_turtle, n_sides, radius):
interior_angle = (180 * (n_sides - 2)) / n_sides
side_length = 2 * radius * math.sin(math.pi / n_sides)
my_turtle.penup()
my_turtle.forward(radius)
my_turtle.left(180 - (interior_angle / 2))
my_turtle.pendown()
draw_polygon(my_turtle, n_sides, side_length)
my_turtle.penup()
my_turtle.left(interior_angle/2)
my_turtle.forward(radius)
my_turtle.left(180)
my_turtle.pendown()

Copy this function to your polygons.py file. Experiment with the procedure to learn what it does. (Hint: Note that this procedure assumes your draw_polygon function draws the first side of the polygon before turning.)

  1. Add an appropriate docstring to this function. Explain the purpose of the function, the parameters, and the overall algorithm.
  2. Explain why each statement in the function's body is necessary to achieve the result that you see. That is, explain each step of the algorithm in English. Write your explanations in comments using #.
  3. Write a void function, centered_polygons(my_turtle, n_sides, initial_radius, additional_radius, n_copies), that draws the given number of copies of the specified polygon. All of the polygons should be centered on the starting location of the turtle. Each polygon should be exactly additional_radius pixels larger than the previous one.

Here are some examples using the centered_polygons function.

centered_polygons(tess, 4, 20, 20, 5)
5 centered squares, initial radius 20, plus 20
centered_polygons(tess, 5, 50, 5, 5)
5 centered pentagons, initial radius 50, plus 5
centered_polygons(tess, 6, 100, -20, 3)
3 centered hexagons, initial radius 100, minus 20

Above & Beyond

In addition to the functions specified above, write a main program that draws something interesting:

When you describe your Above & Beyond component, I encourage you to discuss your creative intentions (what makes the drawing interesting?), as well as the technical skills you exercised.


Submitting your work

Submit two files: termial.py and polygons.py. Make sure these files have the specified names. In your comments at the top, make sure you:

Submit both files using the online turnin form. Be sure to select Homework 2.

Individually complete the assignment Wrapper on Google Forms.


Janet Davis (davisj@whitman.edu)

This assignment is adapted from one developed by Samuel A. Rebelsky, Janet Davis, and Jerod Weinman.

Created January 24, 2016
Last revisedJanuary 29, 2016, 03:47:12 PM PST
CC-BY-NC-SA This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.