Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Java Fun and Games: Explore the geometry of nature

Journey into the realm of fractals

  • Print
  • Feedback

Page 5 of 7

Mandelbrot set

In 1979, Polish mathematician Benoît Mandelbrot began to study a class of fractals known as Julia sets. During this study, he discovered a fractal that has since been named after him. The Mandelbrot set fractal is defined as the set of all complex numbers c such that iterating z = z2 + c (where z is also a complex number) does not escape to infinity.

Complex numbers
A complex number is a number derived from the complex plane. The complex plane is a two-dimensional area of points where the x coordinate consists of a real number and the y coordinate consists of an imaginary number -- a number multiplied by the square root of -1, known as i. Examples: 2 + 3i and -3 - 6i. A real number such as 7 is a complex number whose imaginary component is 0i. Similarly, an imaginary number such as 5i is a complex number whose real component is 0.

Use the following algorithm to recursively generate the Mandelbrot set:

  1. For each point c on the complex plane, repeatedly evaluate z = z2 + c, where z is initially 0.
  2. Determine the distance between the new z and the set's 0 + 0i origin by squaring z's real component, squaring z's imaginary component, adding both squares together, and taking the square root of the sum.
  3. If the distance exceeds 2, the point does not lie in the set and the loop can end (for this point). Otherwise, keep iterating for at least 100 times. When the last iteration completes for a given point, and the distance between the point and the set's origin is still less than 2, display the point at the corresponding (x, y) location on the screen.

To see what the set looks like, check out Figure 5.

The Mandelbrot set.

Figure 5. The Mandelbrot set.

In Figure 5, all points within the set are colored black; all points not within the set are colored white. The x axis serves as both the real number axis in the complex plane (with a minimum of -2.5 and a maximum of 1.5) and the horizontal pixel axis on the screen. The y axis serves as both the imaginary axis in the complex plane (with a minimum of -1.5 and a maximum of 1.5) and the vertical pixel axis on the screen.

Figure 5 does not indicate how many iterations were required to determine that a point is not in the Mandelbrot set. An iteration-based picture is achieved by assigning a non-black color to each iteration, and then displaying a point in this color when the point is found to not be in the set. All points with the same color require the same number of iterations to determine that they are not in the set, as Figure 6 illustrates.

Colors indicate the number of iterations to discover that a point is not in the Mandelbrot set.

Figure 6. Colors indicate the number of iterations to discover that a point is not in the Mandelbrot set.

An applet for the Mandelbrot set

I have created an applet that lets you explore the Mandelbrot set. (See the Resources section for MS.java and MS.html.) Along with the main MS class, this Swing-based applet contains MSPane, a subclass of JPanel. MSPane describes a component that presents the Mandelbrot set via its public void paintComponent(Graphics g) method, as shown in Listing 6.

  • Print
  • Feedback

Resources