| CIT 594 Assignment 3: Recursive Drawings CIT 594, Spring 2007 |
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.)
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.
|
|
|
The general technique is to write a method that takes these parameters:
Graphics element on which to draw (optional--could be
a global). 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.size of the drawing, such as the length of a side.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:
Javadoc your new methods (even if they're private). No JUnit tests in this assignment, since it's all GUI output.
Tuesday, January 30, before midnight.