CPSC 170 Lab 7: Recursion and Fractals

As usual, create a lab7 subdirectory for today's lab, open this document in FireFox, and start eclipse.

About Fractals

A fractal is a geometric figure that is self-similar in that any piece of the figure contains a miniature of the entire figure. Naturally, fractals fit nicely with the concept of recursion.   There are lots of fractals that have interesting mathematical properties (and make pretty pictures); in this lab you will draw two of them, Sierpinski triangles and Koch snowflakes.

Sierpinski Triangles

A Sierpinski triangle (aka a Sierpinkski Gasket) is a fractal that may be constructed as follows:
  1. Draw a triangle.
  2. Draw a new triangle by connecting the midpoints of the three sides of your original triangle. This should split your triangle into four smaller triangles, one in the center and three around the outside.
  3. Repeat (2) for each of the outside triangles (not the center one). Each of them will split into four yet smaller triangles. Repeat for each of their outside triangles.. and for each of the new ones.. and so on, forever. Draw a few rounds of this on paper to see how it works, then check out the demo to see it on screen. Reload to get another triangle.

Your job is to write a Java program that draws a Sierpinski triangle. Think about the following:

When this works, modify it so that when the user clicks, a new, random Sierpinski triangle is drawn. (The demo does this too.) You'll need the usual private inner class to implement the MouseListener interface, and in the mouseClicked method you will need to choose three random points and a random color and repaint. If you followed the guidelines above, nothing will change in your paintComponent or sierpinski methods. 

Print your Sierpinski program.

Koch Snowflakes

The Koch snowflake is a fractal generated by starting with 3 line sements forming an equilateral triangle (a Koch fractal of order 1). The algorithm for generating higher order Koch fractals involves splitting each line segment into three equal segments then replacing the middle segment by two line segments that protrude outward. The same algorithm is then recursively applied to each of the 4 new line segments. In the basic Koch snowflake the two protruding line segments meet at a 60 degree angle. These line segments form an equilateral triangle with the middle segment that is removed. (See the discussion in Chapter 11 for more details.) Files KochSnowflake.java and KochPanel.java contain the program from the text that generates Koch snowflakes (Listings 11.6 and 11.7). Note that this is written as an applet. Copy these files to your directory, compile them, and run the program as an applet to see how it works

In this exercise you will generalize the pattern to allow for triangles other than equilateral ones to be built on the middle third segment. In the drawFractal method this involves changing the calculation of x3 and y3, the coordinates of the protrusion point. The following calculations are equivalent to those currently in the program:

      x3 = (int) (x2 + (cosine * deltaX - sine * deltaY)/3);
y3 = (int) (y2 + (cosine * deltaY + sine * deltaX)/3);

where cosine is the cosine of 60 degrees (which is 1/2) and sine is the sine of 60 degrees (which is the square root of 3 over 2). These equations are generalizable to angles other than 60. In this exercise you will generalize the program to work for angles other than 60. The angle will be controlled by increase and decrease buttons in the same way that the order is currently controlled. Do the following:
  1. In KochPanel.java,

  2. Compile and run the program. It should behave just as before.

  3. To add controls to allow the angle to change, do the following:

  4. Compile and run the program. Play with the angles and order to see what fractal patterns are generated.

Print KochSnowflake.java and KochPanel.java to turn in.

HAND IN:

Hand in Stapled hardcopies of your Sierpinski classes, KochSnowflake.java and KochPanel.java. Tar the files in your lab7 directory and email the .tgz file to me with cpsc170 lab7 in the Subject line.

If you Finish early:

Start looking at Assignment 3