Lab 9: Fractals Due: 11:59 PM on 2020-05-01

This lab has four components, “GCD”, “Stairs”, “Sierpinski”, and “Recursive H”, all of which should be implemented in the same file. Download the skeleton file lab9_fractals.py and replace each pass statement with your own code to complete the function.

The point of these exercises is to give you more practice with recursion, an important concept to be comfortable with in computer science. Once you really understand recursion, the solutions to these problems should be very simple and the code quite short.

Greatest Common Divisor

One of the oldest algorithms, dating back to the 5th century BCE, is Euclid’s algorithm for computing the greatest common divisor (GCD) of two integers. It works as follows:

Write a recursive function gcd(a,b) that implements Euclid’s algorithm. As usual, your function should not use the input or print functions, but rather should take the two integers as parameters and should return a result. Your gcd function should work for any two positive integers sent as parameters. For example:

>>> gcd(36, 81)
9
>>> gcd(13, 6)
1

Stairs

lab9_stairs1.png

Using turtle graphics, write a recursive function stairs(length, levels) to draw pictures such as the one here. Assume the turtle starts in the top left corner, facing right. The first square should have a side length of length, and each subsequent square should have 1/2 the size. There should be a total of levels squares. Do this all in one continuous drawing motion; do not pick up the pen, and do not repeat any lines. Employ the following strategy: (1) draw a half-square, (2) recursively draw the rest of the squares, (3) draw the last half-square to return to the starting position and orientation. Note that powers of 2 work well for starting lengths.

Use the stairs_demo function in the skeleton file for testing.

Sierpinski Triangle

Next we’ll draw another interesting fractal shape called the Sierpinski triangle. Here is what it looks like taken out to six generations:

lab9_sierpinski5.png

There are many ways to draw this shape, but we suggest using the same technique that we used for the Koch curve. Rather than taking a line and breaking it into thirds, here we draw half size triangles. Here is an abstraction of how Generation N of the Sierpinski Triangle is built:

lab9_generationN.png

More simply, here are the first generations 1, 2, 3, 4:

lab9_generations.png

Here is one of the easiest ways to draw the Sierpinski Triangle. Start with the assumption that when you draw the triangle, the turtle will be end up in the same position and orientation it started from. Given that, drawing a generation N triangle just involves drawing three N-1 triangles that are half the size. Draw the shape, move to the location of the next one, draw again, move to the last triangle and draw that one, then restore the turtle to the original location and orientation. For generation 0, don’t do anything (think of the base case of the spiral rather than the Koch curve).

All of this should go in a function called sierpinski(length, iterations). The variable length will be the length of the base of the figure and iterations will tell us how many generations to make.

Use the sierpinski_demo function in the skeleton file for testing.

Recursive H

A recursive H is an H where the end of each vertical line of the H may have another recursive H. The recursive H is defined by a level.

A level 0 recursive H is just a black dot.

A level 1 recursive H is just an H with a dot at the end of each line.

lab9-h1.jpg

A level 2 recursive H has another smaller H at the end of each line:

lab9-h2.jpg

A level 3 recursive H has another smaller H at the end of each of these Hs:

lab9-h3.jpg

Notice that the smallest level of H always ends in a dot (a level 0 H).

Each of these figures is made up of many Hs. An H is defined by a length, length. The vertical bars are of length length and the horizontal bar of length 2 * length.

Assume that the turtle starts in the middle of the drawing window facing to the right. At the end of drawing, the turtle should be at the same position and heading as when it started.

lab9-h1-arrow.png

Write a function named recursive_H that takes two parameters: the length of the vertical bars (the horizontal bar will be twice the length) and number of recursive levels to draw. Level 0 should just draw a dot. Level 1 should just draw an H with four dots at the end, level 2 an H with 4 smaller H’s at the end of the vertical bars. The smallest H’s will all have black dots at the end of their vertical bars.

As shown in the images, the length of the vertical bar (length) should be halved with each level.

Use the recursive_H_demo function in the skeleton file for testing.

Guide

Creativity points

You may earn up to 2 creativity points on this assignment. Below are some ideas, but you may incorporate your own if you’d like. Make sure to document your extra point additions in comments at the top of the file.

Challenge problem 1 [2 Creativity points]

Write a recursive function sum_nested(a_list) that takes in a nested list of integers and returns the sum of all the lists. A nested list of integers contains 0 or more integers and 0 or more nested lists of integers. For example,

>>> sum_nested([])
0
>>> sum_nested([1, 2, 3, 4, 5])
15
>>> sum_nested([1, [2, [3]], 4, 5])
15
>>> sum_nested([1, [2, [3], []], [], 4, 5])
15
>>> sum_nested([[2, 4, 6, 8], [3, 6, 9], [1, [1, 10]]])
50

Hint: The isinstance() function may be of use – run help(isinstance) in your shell.

>>> isinstance([1,2], list)
True
>>> isinstance(1, list)
False

Challenge problem 2 [2 Creativity points]

lab9_stairs3.png

Make a copy of your code from the stairs function and modify it to draw pictures like the one here. Unlike in the Stairs problem, here it is ok to retrace steps or draw the same line twice. Only very slight changes to your code from the Stairs problem should be required. However, your new recursive function should now only have one parameter length, the side length of the overall figure. The line spacing within the figure should be 10 pixels. We used side length of 200 to generate this picture.

Challenge problem 3 [2 Creativity points]

Write a new recursive function (and a helper function that sets up the turtle and calls your function) to draw the Sierpinski carpet. You may find it useful to pick up and put down the pen while doing the drawing. Feel free to enhance your carpet with color! Here are generations 0 through 4:

lab9_carpet.gif

Challenge problem 4 [2 Creativity points]

Use Turtle graphics to draw the recursive leaf shown here

lab9_leaf1.png lab9_leaf1s.png

To help with choosing values for constants, this picture was generated with the following settings:

More challenge: Notice that the leaves on the above image curl both up and down. Find an elegant way to revise your code so that all the leaves curl up as in the recursive leaf shown here

lab9_leaf2.png lab9_leaf2s.png

When you’re done

Make sure that your program is properly documented:

In addition, make sure that you’ve used good coding style (including meaningful variable names, constants where relevant, vertical white space, etc.).

For this lab, when run your program should call no functions and produce no output.

When you’re done, call the function drawing_demo() to run all three drawing functions, and then take a screenshot.

Submit your program and screenshot via Gradescope. Your program file must be named lab9_fractals.py and your screenshot lab9_screenshot.png (produced from the output of drawing_demo()). If you create other recursive drawings you can submit additional screenshots (e.g., named lab9_challenge2.png). You can submit multiple times, with only the most recent submission graded. Note that the tests performed by Gradescope are limited. Passing all of the visible tests does not guarantee that your submission correctly satisfies all of the requirements of the assignment.

Grading

Feature Points
gcd 4
stairs 5
sierpinski 5
recursive_H 6
Comments, style 3
Creativity points 2
Total 25