CS 1 Homework Laboratory # 8
Recursion Practice
Due: Thursday, Nov. 14 for Fienup's section and Friday, Nov. 15 for Jacobson's

Objective: To gain experience using recursion.

Recursive doodling.

This week's assignment has two parts, each of which involves writing a recursive program.

When we doodle, those of us who are less than gifted artistically often resort to making simple geometric patterns on the backs of our notebooks, envelopes, napkins or whatever. One of the all-time favorites is to draw smaller and smaller copies of a single shape inside of another. For example, one can take a shape like a triangle or rectangle and then divide it up into smaller shapes (perhaps by drawing lines between midpoints of adjacent sides) and then divide each of the new regions and so on until all you have is a big blob of ink on the paper.

Fortunately, Java's drawing resolution is good enough that if we get it to do this kind of doodling, we can produce things quite a bit more interesting than a big blob. For example, repeatedly dividing triangles into smaller triangles can lead to a picture like that shown above.

This image was formed by first drawing a triangle and connecting the midpoints. This breaks the triangle into four smaller triangles (at the top, center, lower-left, and lower-right).

The center triangle is then left untouched, but the top, lower-left, and lower-right triangles are further triangulated in exactly the same way until the length of the edges is smaller than 6 pixels long. (In case you are interested, this shape is called Sierpinski's Gasket.)

How to Proceed

The main program (your class which extends WindowController) should just figure out the vertices of the outside triangle and then construct a new object from class TriangleDoodle. The constructor for TriangleDoodle should take as parameters the coordinates of the vertices of the triangle, and the canvas.

The job of the TriangleDoodle constructor is to draw the triangle with the given vertices, and, if the edges are long enough, continue by finding the midpoints of each of the sides and then creating triangle doodles in the top, lower-left, and lower-right portions of the triangle. That's it!

The canvas is 400 pixels square for this problem. You may also find the distanceTo method defined in the Location class useful to solve this problem:

public double distanceTo(Location point)

While the only requirement is that your program be able to draw the gasket, you might want to get extra practice with recursive data structures by adding methods to let you draw multiple gaskets and manipulate them with the mouse.

Part 2:

Your second task is to draw the picture given below:

Let's call this variegated stairs (got to love those big words!). A variegated stairs drawing is constructed by first drawing a large square. Then draw variegated stairs half as large on top of the square, and variegated stairs, also half as large, immediately to the right of the square. The size of your initial square should be a power of 2 (128 is a good choice) so that you don't end up with gaps between the rectangles when you divide the size in half. Stop creating stairs when the squares are less than 3 pixels on a side.

We made it look nicer by drawing the first square in a color created by new Color(225,225,255). At each level of recursion, the color of the first two components (the red and green components) are decreased by 30. That results in nice shading.

The canvas for this problem is 450 pixels wide and 400 pixels tall.

Again, the only requirement is that your program be able to draw the stairs, but you may wish to add extra functionality for extra practice.

Getting Started

1. First you need to locate a free computer in either Wright 112 or 339. Create a new folder "lab8" on the " Student Z: " drive in the folder "cs061" which you created in lab 1.

2. Start Internet Explorer by Start | Programs | Internet | Internet Explorer and go to http://www.cs.uni.edu/~fienup/cs061f02/labs/lab8/.

3. Download each of the files "objectdraw.jar", "lab_8A.gpj", "TriangleController.java," "TriangleDoodle.java," "lab_8B.gpj," "StairsController.java," and "StairsDoodle.java" by right-clicking on the name of each file and selecting "Save Target As". Save these files to your newly created "Z:\cs061\lab8" folder.

4. Start jGrasp by double-clicking on the lab_8A.gpj project file that you downloaded to your new lab8 folder. By using this project file you sets up the correct Compiler and run-time settings for the objectdraw package.

5. Click on the "Project" tab at the bottom of the upper-left-hand sub-window of jGrasp. Double click the "TriangleDoodle.java," and " TriangleController.java" files. Enlarge the "TriangleController.java" window.

6. When compiling, always compile from the "TriangleController.java" (or StairsController.java) window. It will compile all of the .java files for that part.

Submitting Your Work

The submission procedure is basically the same as last week.

Hard-copies, output, and the floppy disk should be turned into the instructor inside the large vanilla envelope. Labs are due at 5 PM on Thursday, November 14, for Professor Fienup's section and Friday, November 15, for Professor Jacobson's section.