COMP 151 – Programming Assignment 2
In this assignment you and your pair programming partner(s) will write a program to generate a tree fractal, as described below. To help you, I have written a program to generate a fractal called the Koch Snowflake. I have also given you my code to draw the tree fractal, minus the actual code that draws the fractal. (What it does have is the framework that gets user input and creates the frame to draw in.)
You have until 8AM, Thursday, March 1 to submit your program via the class website.
You and your partner should submit just one program (either of you can submit). Your program will consist of just one file, Tree.java.
First, I will describe the Koch Snowflake fractal, and I will describe it recursively. This is a review of what I presented in class on the Koch Snowflake. A Koch Snowflake with 0 levels of recursion is just an equilateral triangle:
In a Koch Snowflake with one level of recursion, each side of the triangle is replaced by
- Dividing the side into three segments of equal length.
- Drawing the first and third segments unchanged.
- Treat the middle segment as the base of an equilateral triangle (facing out), and draw the other two sides, but not the base.
This looks like
To get a Koch Snowflake with two levels of recursion, the above rules are applied to each side of the Koch Snowflake with one level of recursion. This looks like
Replacing each side in this drawing using the above rules gives the Koch Snowflake with three levels of recursion, and it looks like
This can continue to an arbitrary number of levels of recursion. My program for generate these images is in a file called KochSnowFlake.java, and you can download it from the assignment page on the class website.
When you run this program, you are prompted to enter two parameters:
- The length in pixels of one side of the triangle that is the 0 levels of recursion snowflake, and
- The number of levels of recursion to display.
Once you enter these values, the Koch Snowflake with 0 levels of recursion is displayed. When you click the mouse, the Koch Snowflake with 1 level of recursion is displayed, and when you click the mouse again, the Koch Snowflake with 2 levels of recursion is displayed. Each mouse click increases the level of recursion by one.
When the maximum level of recursion (the second user parameter) is reached, the next mouse click takes the drawing back to the level 0 recursion. At any time, a double click takes you to the maximum levels of recursion. To terminate the program, close the graphics window that shows the snowflake.
Now, the tree fractal: a tree fractal with just one level recursion is just a single central branch:
In a tree with one level of recursion, the level 0 tree is drawn, plus
- Five branches coming off of the single central branch, alternating left and right, starting with left at the bottom of the tree. The branches are spread out evenly along the central branch. In general, the number of branches coming off the central branch is a parameter to the problem, so it does not have to be five.
- Other parameters to the problems are
-
- The length of the side branches, as a fraction of the length of the central branch. In this drawing, the length is 0.4.
- The angle that the side branches make with the central branch. In this drawing, the angle is 45 degrees.
This tree with one level of recursion looks like
In a tree with two levels of recursion, each of the side branches is itself treated like a central branch, and side branches are drawn off them. The same parameters are used to draw these new branches: number of branches, length of branches, and angle of branches. The resulting tree looks like
Continuing the recursion to three levels gives a tree that looks like
As with the Koch snowflake, the recursion can continue indefinitely.
On the assignment page of the class website is a Tree.java file that contains my solution, with the paintComponent method removed. You should write paintComponent, along with any helper methods that you need, so that Tree.java draws the tree fractal.