CIT 594 Assignment 2: Recursive Drawings
Spring 2009, David Matuszek

# Purpose:

• To give you practice with recursion

# General Idea:

Write six (or more) methods to make recursive drawings. The first drawing should replicate the example below.

In order to allow you to concentrate on the recursion, I am providing starter code that implements a simple GUI. Feel free to modify this code however you like. (I especially recommend putting more meaningful names on the buttons.)

# Examples:

Draw a square (figure 1). Then at each corner of the square, draw a smaller square (figure 2). Then at the corners of each of those squares, draw even smaller squares (figure 3). Continue this process as many times as desired. Figure 1 Figure 2 Figure 3

# How to do it:

The general technique is to write a method that takes these parameters:

• The `Graphics` element on which to draw (optional--could be a global).
• The` x`,` y` location at which to begin drawing. Although most graphics methods (such as `drawRect`) use `x` and `y` as the top left corner of the thing being drawn, you will find it more convenient to use these as the center of the figure you are drawing.
• Some measure of the `size` of the drawing, such as the length of a side.
• A counter to control the `depth` of the recursion (optional--can use `size` instead).

The initial call to this method will be with `x` and `y` representing the center of the drawing area and `depth` representing the maximum depth of the recursion. The method usually does something like the following:

 ```void draw(Graphics g, int x, int y, int size, int depth) { if depth == 0, return draw some figure, centered at x, y, with the given size at various points around the figure { draw(..., a smaller size, depth-1) } }```

The `"at various points`" indicates several recursive calls, possibly in a loop, but more likely just a list of calls. Each call should be with a reduced `depth`, where the base case is a depth of zero (which doesn't do anything).

This is a formula that works well, and you should probably use it to create the above drawings. But once you understand the formula, feel free to try variations on it. For example, you might omit the `depth` parameter, and use as a base a sufficiently small `size`. Also feel free to modify the GUI as needed.

Here are some things you might try:

• Use colors.
• Use filled figures, not just outline figures.
• Do something different at alternate levels (depending on whether depth is even or odd).
• Pass in extra parameters to vary other things.

Javadoc your new methods (even if they're private). No JUnit tests in this assignment, since it's all GUI output.

• Be sure to use as parameters both the location (as` x`, `y`) and the `size` of the drawing, and don't get these confused. Size and location are independent, and must be treated as such.
• To compute a new location for the recursion, do not multiply or divide `x` or `y` by anything. Always use the current `x`, `y` location plus or minus some function of the `size`. (If you do a little arithmetic at different locations, you will see why this is important.)
• Use `depth - 1` in each recursive call, rather than setting `depth = depth - 1` before all the calls. Either way usually works, but the former is a little safer.
• Drawings with diagonal lines sometimes require a bit of trigonometry. Use or avoid, depending on your comfort level.
• If you are using integer arithmetic, remember that division isn't always exact. This will affect the smallest parts of your drawing. If it looks good, don't worry about it. If it looks bad, try tweaking your initial `size`.
• Avoid global variables!

These are not intended as additional requirements. If you get an interesting pattern (probably accidentally!) by violating some of these, feel free to keep it. # Due Date:

Thursday, January 29, before midnight.