Week 2, Problem 3: Python
Bonsai
[30
points + 15 points (extra); individual or pair] filename: hw2pr3.py
This problem asks you to
create two classic kinds of recursive images: a spiral and a tree made of line
segments (and optionally the Koch Snowflake). If you are so inclined, you can
make your tree more realistic than just a stick tree and get extra credit.
On a typical computer, the turtle
drawing module is installed and ready to use. (If for some reason your computer
doesn't have turtle
installed, then you can use the csturtle
module instead. To learn
more about how to get csturtle
, go here.)
To learn how to use turtle
and to see all of it's commands, click here.
Note:
When you run a program with turtle commands, a special window will open where
the drawing will take place (This
window may "hide," appearing BEHIND the other windows).
If you run the program again, that same window will be used again. However,
when you want to finally close that drawing window, you must first type done()
at the IDLE prompt. Then you can close the window (for example, by clicking on
the red circle at the top left of the window on a Mac).
To simplify grading, please be sure to name your
functions as specified in these problems... . The graders thank you!!
Part
1
Write a function
spiral( initialLength, angle, multiplier )
that uses the csturtle
drawing functions to
create a spiral that has its first segment of length initialLength
and whose neighboring segments form angles of angle
degrees. The multiplier
will be a float that will indicate how each segment changes in size from the
previous one. For example, with a multiplier
of 0.5
each side in the spiral should be half the length of the previous side.
The spiral should stop drawing when it has reached a
side length of less than 1 pixel or greater than 1000 pixels... .
Part
2
"The fallen tree" the idea here is to create
a function that draws the side-view of a tree:
svTree( trunkLength, levels )
Here is an example of the output from my function when svTree( 128, 6 )
is run:
and another example of the output when svTree( 50, 2 )
is run:
Note that these are really side view! Of course, calling left(90)
before the call to svTree
will yield a more traditional pose... .
The key thing to notice about both of these pictures is
that the pen is back at the start (root) of the tree at the end of the
function call! This is the key to all happiness when specifying
recursive images!
One thing not
to worry about is the number of branches (anything greater than 1 is OK), the
exact angle of branching, the reduction of the trunkLength
in sub-branches, etc. Design your own tree by making aesthetic choices for each
of these.
Submission
You
should submit your hw2pr3.py file at the Canvas.
Not
enough? Extra credit options...
Choice 1: Your own recursive designs...
[15 points, individual or pair]
For extra credit of up to 5 points, feel free to design
and implement your own recursive artwork. One starting point might be
"sprucing up" your svTree
function further by adding other cool
features. We're "pining" for some interesting variants. If you do add
extra features to your tree -- or create any other work -- make sure to
"fir"nish us with a comment at the very top of your code indicating
what you did and how to invoke your function.
Consider using the fill and color
settings or changing the theme away from trees completely. The CS turtle reference page
provides documentation for the functions available from the turtle
module.
See the description of a function name on that page.
Choice 2: The Koch curve
[15 points, individual or pair]
A more focused task for an additional possible 5 points
extra credit is to create a function named
snowflake(sideLength, levels)
The idea is that snowflake
will draw levels
recursive levels of the Koch Snowflake, a fractal curve that is described here, among other
places on the web. Basically, a Koch snowflake of level 0
is simply an equilateral triangle with side length equal to sideLength
.
Each increment of level
replaces the middle third of all of the snowflake's sides with a
"bump" that is, itself, a smaller equilateral triangle.
One hint that may or may not be helpful to you is that
in our solution, we write a helper function called snowflakeSide( sideLength, levels )
that draws just one side of the underlying equilateral triangle. Then, the snowflake
function was quite similar to the tri() example from class. The key recursion
occurs in snowflakeSide.
This division of labor is not required, however - there are a number of
alternative ways to organize it.
Here are four steps of the Koch curve's progression:
Submission
You
should submit your hw2pr3.py file at the Canvas.