CPSC 170 Lab 7: Recursion and 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 Sierpinski 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 step 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.

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

The midpoint of a line segment can be calculated using interpolation. In order to find a point that is some fraction between the two points (x1, y1) and (x2, y2) the following equations can be used:

xi = i * x2 + (1 - i) * x1
yi = i * y2 + (1 - i) * y1

where i is a value between 0 and 1 that is the fraction of the distance between the two points that the point (xi, yi) is located. So, in order to find a point that is 1/2 between two points a value of i = 0.5 could be used.

The file SierpinskiTriangle.java contains the skeleton of a program to draw a Sierpinski triangle. The program has a slider that allows the user to specify the depth of the triangle that is drawn to the screen. The paint method simply calls the sierpinski method, which you must complete. The method should draw the triangle whose points were passed in, and then check to see if the desired depth has been achieved; if not, it should draw the triangle formed by the midpoints of the given points, then call itself recursively on each of the three new outer triangles. Try changing the color of the triangles and the location of the vertices. Use the slider to create fractal that you like. Use the Applications>Accessories>Take Screenshot application to create an image of the fractal and put it in your lab7 directory.

Koch Snowflakes

The Koch snowflake is a fractal generated by starting with 3 line segments forming a 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. The diagram below illustrates the transformation of a segment.

Koch Diagram

In order to calculate the four line segments that represent the transformation of the segment defined by the points (x1, y1) and (x5, y5) the points that divide the segment into thirds, (x2, y2) and (x4, y4), must be calculated. These can be calculated using interpolation and the equations from the previous section of this lab.

The point that is the apex of the protrusion, (x3, y3), can be calculated with the following equations:

x3 = x1 + Δx / 2 - Δy * p
y3 = y1 + Δy / 2 + Δx * p

where

Δx = x5 - x1
Δy = y5 - y1

and p is a value between -1 and 1 that is the percent length of the original segment that the protrusion will be. That is, if p is 0.5 then the protrusion would be half the size of the segment from (x1, y1) to (x5, y5). If p is -0.5 then it would also be half the size of the segment, but protrude off of the other side of the segment. A value of one third of a segment length produces a nice looking fractal.

The file KochSnowflake.java contains the skeleton of a program to draw a Koch snowflake. The program has sliders that allow the user to specify the order and the size of the protrusion of the snowflake that is drawn to the screen. The paint method simply calls the koch method, which you must complete. The method should check to see if the desired depth has been achieved. If it has, it should draw the line of the given points. If it has not, then it should compute the four line segments that make up the next order and call itself recursively on each. Try changing the color of the triangles and the location and number of the initial lines. Use the sliders to create a fractal that you like. Use the Applications>Accessories>Take Screenshot application to create an image of the fractal and put it in your lab7 directory.

To submit your code: Tar your lab7 directory and submit your code on the course Inquire site.