Homework 5: Playful Polygons

Assigned
Friday September 16, 2016
Due
11 p.m., Monday September 19, 2016
Summary
The goal of this assignment is to gain more experience writing and using functions.
Collaboration
Do this assignment with your assigned partner.
Submitting
Submit a Python program using the online turnin form. See below.
Scoring
15 points

Assignment

Create a file named polygons.py.

Part 1. A helpful function

As a first step towards drawing polygons in general, write a function calc_angle that takes a number of sides as an argument, and returns the number of degrees in the angle that the turtle should turn when drawing such a polygon. (For example, for a triangle, it should return 120, as we saw in class.)

Part 2. Drawing a generalized polygon

Write a function draw_polygon(tortoise, n_sides, side_length) that has the given turtle draw a regular polygon with the given number of sides and the given length. Your function must return the turtle to its original position and angle, and it must not change the turtle’s color.

Use the calc_angle function that you wrote for part 1 to determine how far to turn the turtle as you draw these polygons.

Part 3. Spinning polygons

Write a function spin_polygon(tortoise, n_sides, side_length, angle, n_copies) that draws the specified number of copies of a polygon (using your draw_polygon function,) with the turtle turned by angle between each of the polygons.

Here are some examples of this function being called on a turtle named kat:

spin_polygon(kat, 4, 50, 30, 5)
spin example

spin_polygon(kat, 4, 50, -30, 3)
spin example

spin_polygon(kat, 4, 5, 10, 360 // 10)
spin exmple

As in the previous problems, your function must return the turtle to its original position and heading.

Part 4. Scaling polygons

Write a function scale_polygon(tortoise, n_side, side_length, scale_factor, n_copies) that draws a given number of copies of the specified polygon, with each copy drawn with a side length scale_factor times the previous side length.

For example, if the initial side_length is ten and the scale_factor is two, the side lengths of drawn polygons should be 10, 20, 40, 80, …

Here are some examples of this function being called on a turtle named tess:

scale_polygon(tess, 5, 10, 2, 4)
scale example

scale_polygons(tess, 5, 1, 1.2, 25)
scale example

As in the previous problems, your function must return the turtle to its original position and heading.

Submitting your work

Submit one file, polygons.py, using the turnin form.

Point distribution


Janet Davis (davisj@whitman.edu).
This assignment was adapted by Andy Exley from one written by Janet Davis, Samuel A. Rebelsky, and Jerod Weinman.

Created September 15, 2016
Last revisedSeptember 15, 2016, 12:25:22 PM PDT
CC-BY-NC-SA This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.