Chat with us, powered by LiveChat create a "tree" using recursion | Writedemy

create a “tree” using recursion

create a “tree” using recursion

Question

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

  1. Dividing the side into three segments of equal length.
  1. Drawing the first and third segments unchanged.
  1. 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:

  1. The length in pixels of one side of the triangle that is the 0 levels of recursion snowflake, and
  2. 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

  1. 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.
  2. Other parameters to the problems are
    1. The length of the side branches, as a fraction of the length of the central branch. In this drawing, the length is 0.4.
    2. 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.

Our website has a team of professional writers who can help you write any of your homework. They will write your papers from scratch. We also have a team of editors just to make sure all papers are of HIGH QUALITY & PLAGIARISM FREE. To make an Order you only need to click Ask A Question and we will direct you to our Order Page at WriteDemy. Then fill Our Order Form with all your assignment instructions. Select your deadline and pay for your paper. You will get it few hours before your set deadline.

Fill in all the assignment paper details that are required in the order form with the standard information being the page count, deadline, academic level and type of paper. It is advisable to have this information at hand so that you can quickly fill in the necessary information needed in the form for the essay writer to be immediately assigned to your writing project. Make payment for the custom essay order to enable us to assign a suitable writer to your order. Payments are made through Paypal on a secured billing page. Finally, sit back and relax.

Do you need an answer to this or any other questions?

About Writedemy

We are a professional paper writing website. If you have searched a question and bumped into our website just know you are in the right place to get help in your coursework. We offer HIGH QUALITY & PLAGIARISM FREE Papers.

How It Works

To make an Order you only need to click on “Order Now” and we will direct you to our Order Page. Fill Our Order Form with all your assignment instructions. Select your deadline and pay for your paper. You will get it few hours before your set deadline.

Are there Discounts?

All new clients are eligible for 20% off in their first Order. Our payment method is safe and secure.

Hire a tutor today CLICK HERE to make your first order