for
loops for iteration.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.
Implement the following functions in a single file named polygons.py
.
polygons.py
program and run the program. As always, don't be satisfied with just
one test. Write your own tests to convince yourself the function works
as required.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.
Write a void function,
,
that draws the
given number of copies of the specified polygon by calling your spin_polygon
s(my_turtle,
n_sides,
side_length,
angle,
n_copies
)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.
Write a void function,
, that draws
the
given number of copies of the specified polygon, with each copy drawn
with a side length scale_polygons(
my_turtle,
n_sides,
initial_side_length,
scale_factor
,
n_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 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.
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.)
#
.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.
In addition to the functions specified above, write a main program that draws something interesting:
for
loops in different ways.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.
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.
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
This work is licensed under a Creative
Commons Attribution-Noncommercial-Share Alike 3.0 United States License.