CS 106 Winter 2018

Assignment 06: Recursion


Question 1 Pythagorean Tree

Imagine drawing a square. On the top edge of the square, arrange two more squares, tilted at 45 degree angles, so that they each touch one corner of the original square and meet above it, like this:



Now, keep doing the same thing: add four grandchildren, two squares on the tops of each of the two child squares. Add eight great-grandchildren, and so on to some predetermined number of levels. The theoretical limit of this process will be a kind of fractal known as a Pythagorean Tree.

Let's change the design slightly, so that instead of a square we draw a house-shaped pentagon that looks like a triangle glued onto the top of the square. (The triangle should correspond to the gap between the squares above, so that the angle at the peak of the roof is 90 degrees.) We'll glue child houses onto the sloped sides of the roof of each parent house. If we say that the "Level 0" drawing is just one such house, then the first seven levels of this process might look like this:

In the provided starter code, open the Tree sketch. The sketch manages the number of recursive levels for you—use the left and right arrow keys to decrease and increase the number of levels. Of course, in the starter code, nothing is drawn. Fill in the tree() function so that it draws a house-based Pythagorean tree that looks like the seven examples above. Here are some tips for doing so:

Save your finished sketch in your A06 folder with the name Tree.

Question 2 Mondrian


Piet Mondriaan abstract painting Composition II in Red, Blue, and Yellow, 1930
By Piet Mondrian - [1], Public Domain, Link


Piet Mondrian was one of the most important artists of the 20th century. His neoplasticism style contributed greatly to the growth of abstract art. Many paintings in this style use an extremely limited artistic vocabulary: horizontal and vertical black lines divide the canvas into rectangles, some of which are filled with red, yellow, or blue. In this exercise, we'll use recursion to create a sketch that replicates some of the appearance of Mondrian's work, in a limited way.

In the provided starter code, open the sketch titled Mondrian. You'll see a general structure not unlike that of the previous question, with a global number of recursive levels controllable with the left and right arrow keys. This sketch also redraws the sketch window at the current level if you press the space bar.

In this sketch the mondrian() function takes a number of parameters as input:

Complete the mondrian() function. You shouldn't have to change any other part of the sketch. Your function should work roughly as follows:

Save your finished sketch in your A06 folder with the name Mondrian.

Submission

When you are ready to submit, please follow these steps.

  1. Please ensure that any sketches you submit compile and run. It's better to submit a sketch that runs smoothly but implements fewer required features than one that has broken code for all features. If you get partway into a feature but can't make it work, comment it out so that the sketch works correctly without it.

  2. If necessary, review the Code Style Guide and use Processing's built-in auto format tool. You do not need to use the precise coding style outlined in the guide, but whatever style you use, your code must be clear, concise, consistent, and commented.

  3. If necessary, review the How To Submit document for a reminder on how to submit to LEARN.

  4. Make sure to include a comment at the top of all source files containing your name and student ID number.

  5. Create a zip file called A06.zip containing the entire A06 folder with its subfolders Tree and Mondrian.

  6. Upload A06.zip to LEARN. Remember that you can (and should!) submit as many times as you like. That way, if there's a catastrophe, you and the course staff will still have access to a recent version of your code.

  7. If LEARN isn't working, and only if LEARN isn't working, please email your ZIP file to the course account (see the course home page for the address). In this case, you must mail your ZIP file before the deadline. Please use this only for emergencies, not "just in case". Submissions received after the deadline may receive feedback, but their marks will not count.