Java Reference
In-Depth Information
27
public static void main(String[] args)
28
{
29
int startPeg = 1 ; // value 1 used to indicate startPeg in output
30
int endPeg = 3 ; // value 3 used to indicate endPeg in output
31
int tempPeg = 2 ; // value 2 used to indicate tempPeg in output
32
int totalDisks = 3 ; // number of disks
33
34
// initial nonrecursive call: move all disks.
35
solveTowers(totalDisks, startPeg, endPeg, tempPeg);
36
}
37
} // end class TowersOfHanoi
1 --> 3
1 --> 2
3 --> 2
1 --> 3
2 --> 1
2 --> 3
1 --> 3
Fig. 18.11 | Towers of Hanoi solution with a recursive method. (Part 2 of 2.)
18.9 Fractals
A fractal is a geometric figure that can be generated from a pattern repeated recursively
(Fig. 18.12). The figure is modified by recursively applying the pattern to each segment of
the original figure. Although such figures had been studied before the 20th century, it was
the mathematician Benoit Mandelbrot who in the 1970s introduced the term “fractal,”
along with the specifics of how a fractal is created and the practical applications of fractals.
Mandelbrot's fractal geometry provides mathematical models for many complex forms
found in nature, such as mountains, clouds and coastlines. Fractals have many uses in
mathematics and science. They can be used to better understand systems or patterns that
appear in nature (e.g., ecosystems), in the human body (e.g., in the folds of the brain), or
in the universe (e.g., galaxy clusters). Not all fractals resemble objects in nature. Drawing
fractals has become a popular art form. Fractals have a self-similar property —when sub-
divided into parts, each resembles a reduced-size copy of the whole. Many fractals yield an
exact copy of the original when a portion of the fractal is magnified—such a fractal is said
to be strictly self-similar .
18.9.1 Koch Curve Fractal
As an example, let's look at the strictly self-similar Koch Curve fractal (Fig. 18.12). It's
formed by removing the middle third of each line in the drawing and replacing it with two
lines that form a point, such that if the middle third of the original line remained, an equi-
lateral triangle would be formed. Formulas for creating fractals often involve removing all
or part of the previous fractal image. This pattern has already been determined for this
fractal—we focus here on how to use those formulas in a recursive solution.
 
 
 
Search WWH ::




Custom Search