# Assignment 7: Recursive Fractals

Due: Tuesday, December 14th by 11:59 PM

## Getting started

Start by importing the file CS201_Assign7.zip into Eclipse: File->Import->Existing Projects into Workspace, click Select archive file, select CS201_Assign7.zip from the file selection dialog, and then click the Finish button. You should see a project called CS201_Assign7 in the Package Explorer.

## Fractals

In this assignment you will define recursive methods for drawing several kinds of simple fractals using turtles. A fractal is a geometric object that is irregular at all levels of detail. The fractals in this assignment 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:

• void move(double distance) - move distance units of distance in the direction indicated by the turtle's current orientation
• void turn(double direction) - add the given direction to the turtle's orientation. For example, to turn clockwise 90 degrees, call turn(Math.PI / 2). To turn 90 degrees counter-clockwise, call turn(-(Math.PI / 2)).
• Turtle branch() - return an exact duplicate of the turtle this method is called on.

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 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

• The "S" indicates where the fractal begins. (A turtle will start at this point.)
• The solid black lines are line segments to be drawn using the original turtle, or a turtle created by calling the branch method on the original turtle. The first segment of the fractal should be drawn using the original orientation of the turtle.
• The blue dashed lines are smaller, recursively drawn fractals. They should be drawn by making a recursive call to your fractal-drawing method, decreasing a distance factor.

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

(Click any of the above images 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".