CS150 Lab 6:  Recursion, Recursion, Everywhere Recursion!
or
Chaotic Koch Curves!
State Pattern
Abstract Factories
Home Java Resources Discussion Dropoff

Due Sun. Nov. 5, 11:59 PM

In this lab we will explore the use of recursion in creating a fractal patterns.  We will also encounter two design patterns: the State pattern (hello again! :-) ) and the Abstract Factory pattern.     A true mutable, object oriented recursive list, LRStruct, will be used, and we will take advantage of its powerful capabilities.

Koch Curves

Fractals are an important area of scientific study as it has been found that fractal behavior manifests itself in nature in everything from broccoli to coastlines. 

A Koch curve is a fractal curve that can be constructed by taking a straight line segment and replacing it with a pattern of multiple line segments.    Then the line segments in that pattern are replaced by the same pattern.   Then line segments in that are replaced,... etc., etc., etc ...  

Successive iterations through the construction of a Koch curve.

Three Koch curves in a triangle makes a "Koch Snowflake":

Clearly, the generation of a Koch curve is a prime candidate for recursion.

Also, in looking at the the operational definition of a Koch curve, we can see that if we examine the curve between any two vertices (corners) of the curve (for a given, finite iteration level), there are two possible states:

  1. The curve is a single straight line between the two points.
  2. The curve is a collection of multiple straight lines between the two points.

In particular, for #2 above, the collection of straight lines is composed of the repeated line pattern of smaller Koch curves.   Thus we can create a recursive definition of a Koch curve:

A Koch curve between two points is:

  1. A straight line between the two points, or
  2. A pattern of intermediate points with a Koch curve between each successive pair of points.

A Koch curve contains a Koch curve which contains a Koch curve which contains....until finally, the curve is a straight line.   Does this sound familiar?   It should.

Notice as well that when the curve "grows" from one iteration to the next, that all the straight line segments transition from the base case Koch curve (straight line) to the non-base case Koch curve (a pattern of Koch curves) which is the series of straight lines.     The Koch object appears to change its class because it has new behaviors for the same methods and same property values.  "Dynamic reclassification" and all that...where have we seen that before?

Laboratory developed by Stephen Wong and Antonio Garcia