CSC 210 Program #1

Recursive trees

Logistics

Details, schmetails...

  •  Worth 4 points, or 4% of your class grade
  •  Assigned Saturday April 1, 2006
  •  Due Saturday April 8, 2006 by the end of the day
  •  Covers recursion as discussed in Chapter 7 and our first lecture

We'll get back in the saddle of Java GUI programming with Program #1. I hope to get y'all in a Java development groove using: jGRASP, swing, applets, javadoc, HTML, etc.

Description

In Program #1, we're going to draw some recursive trees, like this one:

Starting with the trunk, each branch spawns two new branches just like itself, but with the following changes:

  •  Of course, the line for each new branch starts at a new (x,y) coordinate which is the end of the previous branch
  •  The length of a new branch is reduced by an input multiplicative factor... I called it lengthFactor
  •  The angle of a new branch is changed by another input factor... angleDelta

The tree example above has an order or recursive depth of 5... notice the 5 levels of branches. BTW, also notice that the tree is symmetric. The lengthFactor is 0.80 and the angleDelta is 33 degrees.

Pseudo-code for the recursive tree algorithm is:

// recursively draw tree with depth levels, starting at (x,y)
// length pixels long, with 2 branches at angle
rtree( depth, x, y, length, angle)
  if depth is 0
    then return
  else
    dx = length * sin( angle)
    dy = length * cos( angle)
    draw line from (x,y) to (x+dx,y+dy)
  
    // for the left branch
    rtree( depth-1, x+dx, y-dy, length*factor, angle+angleDelta)
    // for the right branch
    rtree( depth-1, x+dx, y-dy, length*factor, angle-angleDelta)

To satisfy your right-brain on this program, please use the Random class to add some randomness to your trees. Some ideas:

  •  Randomly change branch colors
  •  Randomly change branch lengths within some bounds
  •  Ditto for branch angles
  •  Maybe you randomly prune a certain percentage of branches altogether
  •  Try three branches... one where the angle doesn't change
  •  Put red berries on the end of the branches
  •  Whatever!

It's up to you. Also, if you get ambitious (or tired of changing parameters in your main program all the time), then you can add GUI objects to your program to change the tree interactively.

Implementation

So how do we do all this? Well, we're going to discuss this in class. We'll touch on:

  •  The organization of your program. I'll show you mine for our first program.
  •  Placing your first branch and then recursively drawing the rest... my first (x,y) is based on the JFrame size: (width/2, height) and my first length is height/4. This is not set in stone.
  •  The Random class
  •  Java applications using main() versus Java applets. You can do either... or both. It's pretty easy.
  •  Using jGRASP

One of the things that you'll have to do is convert from degrees to radians because degrees (like 45 degrees) work with polar coordinates and radians work with our good, old Cartesian (x,y) coordinate plane. Here's a little method, degreesToRadians(), that you'll need for this translation:

/** Converts degrees into radians, for use in our Cartesian
   coordinate system (x,y) world.
   @param degrees The angle to be converted to radians
   @return The value of the angle in radians
*/
private double degreesToRadians( double degrees)
{
   return degrees * Math.PI / 180;
}

Grading

By the due date, please place your work for Program #1 in your folder on the k: drive. I'll be looking for:

  •  A README file describing the state of your program... what works, what doesn't, etc.
  •  All your *.java Java source code files
  •  All your *.class files... please compile your Java
  •  A javadoc folder if you're using jGRASP; if you're not using jGRASP, then show me what NetBeans can do... here's mine: program01/rtree_doc/index.htm
  •  index.htm... A web page with an Applet of one of your favorite trees... for example, here's mine for you to use/copy if you like: program01/index.htm

Of course, your code must be beautiful and follow the class coding guidelines. Code that does not meet this metric will be served a harsh brand of grading justice.

Etc

I will be helping you more on this program, as it is our first. Along those lines, look in the k: drive common_area for:

  •  my Template classes that provide a nice starting point for your work
  •  the Javadoc pages for my program #1 solution

This program is somewhat similar to the Koch snowflake in your 161 text, Lewis & Loftus (L&L), page 596.

There's a nice description of the Random class with a couple of examples in L&L, page 124.

I liked the simplicity of the Applet shown on page 96 of L&L.

Appendix C in our text was a nice overview of graphics programming in Java, but it's overkill for Program #1. The classes I use are: JPanel, JApplet, JFrame, Graphics, and Color.

As an aside, I'm currently thinking that we'll have 5 programs this term at 4 points a pop. They should all be small/medium-sized... though probably a little larger than this one. Enjoy!

Apr 5, 2006

Hey the line in the pseudo-code above for the recursive tree algorithm:

draw line from (x,y) to (x+dx,y+dy)

should read:

draw line from (x,y) to (x+dx,y-dy)

The recursive calls to rtree() in the pseudo-code do this correctly.

thanks... yow, bill

 

...

william.krieger.faculty.noctrl.edu

wtkrieger@noctrl.edu