Java Reference
In-Depth Information
different signatures (one parameter versus two). We make the second method a
private method because we don't want to clutter up the public interface seen by
the client.
There is a final benefit to this two-method approach. Sometimes you want to per-
form an action once, either before or after the recursive method executes. By dividing
the recursive method into a public and a private method, you can include this one-
time code in the public method either just before or just after the call on the private
method.
12.4 Recursive Graphics
There has been a great deal of interest in the past 30 years about an emerging field of
mathematics called fractal geometry. A fractal is a geometric object that is recur-
sively constructed or self-similar. A fractal shape contains smaller versions of itself,
so that it looks similar at all magnifications.
Benoit Mandelbrot founded the field of fractals in 1975 with his first publication
about these intriguing objects, particularly a specific fractal that has come to be
known as the Mandelbrot set. The most impressive aspect of fractal geometry is that
extremely intricate and complex phenomena can be described with a simple set of
rules. When Mandelbrot and others began drawing pictures of their fractals, they
were an instant hit.
Many fractals can be described easily with recursion. As an example, we will
explore a recursive method for drawing what is known as the Sierpinski triangle. We
can't draw the actual fractal, because it is composed of infinitely many subtriangles.
Instead, we will write a method that produces various levels that approximate the
actual fractal.
At level 1, we draw an equilateral triangle as shown in Figure 12.1:
Figure 12.1
Sierpinski triangle, level 1
Proceeding to level 2, we draw three smaller triangles that are contained within the
original triangle (see Figure 12.2):
 
Search WWH ::




Custom Search