Java Reference
In-Depth Information
3.7 Recursion and graphics **
Beautiful recursive geometric patterns are often observed in nature. To give a
single example, consider the shapes of snowflakes: They look similar to Koch's
mathematical snowflakes illustrated in Figure 3.4. These patterns are called
fractal patterns since they are defined by a recursive process.
Figure 3.4 Koch's mathematical
recursive snowflakes
One famous fractal mathematician is Waclaw Sierpinski (1882-1969), who
studied such recursive patterns. For example, the Sierpinski's triangle recursive
pattern is given by the following simple rewriting rule:
Replace a given (parent) triangle by three (children) triangles defined by the
midpoints of the edges of the parent triangle. Figure 3.5 shows Sierpinski's
fractal triangle obtained after a few recursive graphics rewriting operations.
The figure was produced by the following more elaborate Java program which
recursively draws the fractal pattern using procedure sierpinski draw .
Program 3.16 Sierpinski's fractal triangles
import javax . swing . ;
import java .awt . ;
public class Sierpinski extends JFrame {
public static final int WINDOW SIZE = 512;
public static final int THRESHOLD= 1 0 ; // stopping criterion
for recursion
public static int P1 x, P1 y, P2 x, P2 y, P3 x, P3 y;
public Sierpinski () {
super ( "Sierpinski" );
setSize(WINDOWSIZE, WINDOW SIZE) ;
// A simple triangle
P1 x=( int )getSize () .getWidth() /2;;
P1 y = 40;
P2 x = 20;
P2 y=( int ) getSize () . getHeight ()
20;
P3 x=( int )getSize().getWidth()
20;
 
 
Search WWH ::




Custom Search