CMPU 102 - Lab 7

In this lab you will define recursive methods for drawing several kinds of simple fractals.  A fractal is a geometric object that is irregular at all levels of detail.  The fractals in this lab are also self-similar, in that the structure of the fractal is similar at all levels of detail.

The Turtle interface defines the operations of turtle objects.  A turtle has a location (x and y coordinates) and an orientation (which way it is facing).  When you move the turtle d units of distance, it draws a line from its current location to a point d units away, in the direction indicated by the turtle's orientation.

The turtle's orientation is specified by an angle (in radians) starting from 0, at which point the turtle is facing straight up.  As the angle increases, the turtle rotates clockwise.  At 2 pi radians, the turtle has rotated through a full circle and is pointing up again.  The following diagram explains how the orientation works:

The Turtle interface contains the following methods:

The move and turn methods allow you to draw a continuous path without any splits.  If you want to split the path you are drawing, you will need to call the branch method to create a duplicate of the current turtle.  The new turtle returned by branch may then be turned and moved independently of the original.

Your task

Your task will be to implement code to draw several kinds of fractals: "Trees", "Ferns", and (if you have time) "H-Trees".  These fractals are drawn by the Tree, and Fern, HTree classes.  The general idea is as follows: at each step

  1. Draw one or more lines
  2. Recursively draw one or more smaller versions of the fractal connected to those lines

The fractals are drawn according to the following rules:

In each case

Each fractal-drawing method will have a minimum segment length to serve as a base case for the recursion.  Once the distance passed to the method falls below the minimum, it should return without drawing anything.

Details

Tree Fern H-Tree
(Click on small image to see full-size image)

Tree is the simplest fractal.  It is just a straight segment, with two sub-trees branching off of it.  In the Tree class, you will see constants defining the angle to turn for each branch, and the factor by which to decrease the segment distance for the recursively-drawn branches.

Fern is a more compex fractal.  It consists of a main "spine", with smaller "branch" sub-ferns branching off the side of the spine.  The spine of the fern curves: the right branch curves to the left, and the left branch curves to the right.  The spine of the main fern also curves (as indicated by a parameter to the method that draws the fern.)  In the Fern class, you will see constants indicating how to generate the branches and the continuation of the spine as recursive calls.  (Both the branches and the continuation of the spine are recursive sub-ferns, but the branches are significantly smaller.)

The H-Tree fractal is a way to compactly lay out a binary tree on a two-dimensional surface so that none of the edges (lines) cross.  It consists of interlocking shapes that look like the letter "H", where each generation of shapes is half the size of the original.  The HTree class is set up so that you only need to draw half of one of the "H" shapes, which will look like a "T".  A method called reflect takes care of calling the method that draws the "T" shape twice in opposite directions in order to form an "H".

Getting started, when you are done, submitting

Import lab7.zip into your Eclipse workspace.  You can find it (as well as this description) at the bottom of the class web page.

When you are done, show me your Tree and Fern fractals, and your H-Tree if you have implemented it.  (You will earn extra credit for finishing the H-Tree.)

To submit: from a terminal window

cd
cd eclipse-workspace
submit102 lab7