The Mandlebrot Set

The image below is a section of the famed fractal, the Mandelbrot Set. Fractals are strange sets whose dimension often lies between integers -- that is to say, something more than a line, but not quite a surface; or more than a surface, but not quite a volume; and so on...

As it happens, writing a program to draw the Mandelbrot Set is actually very easy to do with a programming language like Java -- as long as you have a basic understanding of complex numbers. With that in mind, recall a complex number is any number of the form $a+bi$ where $a$ and $b$ are real numbers, and $i = \sqrt{-1}$. So for example $(1.2+0.7i)$ is a complex number.

These numbers are added, subtracted, and multiplied together in a natural way (i.e., we treat them like polynomials in $i$, and remember that $i^2 = -1$. For example:

$(1+3i) + (2 - 5i) = (3 - 2i)$
$(1+3i) - (2 - 5i) = (-1 + 8i)$
$(1+3i)(2-5i) = 2 - 5i + 6i - 15i^2 = 2 - 5i + 6i + 15 = (17+i)$

Suppose the central drawing area of the breadboard represents the part of the complex plane where both the real part (the value of $a$ above) and the imaginary coefficient (the value of $b$ above) fall between -2 and 2. Suppose $c$ is the complex number that corresponds to some pixel in this area. For each such pixel and complex number $c$, we create a sequence of complex values $z_0, z_1, z_2, z_3, \ldots$, such that

$z_0 = 0$ and $z_{n+1} = z_n^2 + c$

We say the sequence thus produced from a starting complex value "escapes to infinity" if it contains a term whose magnitude (i.e., distance to the origin) exceeds $2$, as it can be easily proven that the later terms of the sequence start to get farther and farther away from the origin once this happens. Suppose we are interested in seeing which points of the complex plane produce sequences that escape to infinity, and which ones don't...

As a practical matter -- since we can't actually calculate the infinitely many terms of each sequence in finite time -- suppose we calculate $M$ terms of each sequence. If none of the $M$ terms for a particular sequence have magnitude greater than $2$, we color the pixel corresponding to that sequence black. Otherwise, we color the pixel some other color.

Often, for pixels that correspond to sequences that escape to infinity, one chooses the color according to how long it takes to produce the complex term with magnitude (i.e., distance to the origin) is greater than $2$. Calculating the pixel content/color in the following way will result in an aesthetically pleasing image:

//supposing the nth term is the first in the sequence with magnitude exceeding 2...
int blue = 35 + 10 * (n % 23);
int red = 75 + 10 * (n % 19);
int green = 95 + 10 * (n % 17);

The Mandelbrot Set is defined to be the set of complex values that produce sequences that fail to escape to infinity. Write a class named that extends the TwoButtonBreadboard class, and plots the Mandlebrot set in black, while coloring the rest of the planar region shown on the canvas using the algorithm above or another one of your own design.

Additionally, if the user clicks-and-drags a rectangle and then clicks the "Zoom" button, the region shown on the canvas should be updated accordingly. If a number has been entered in the breadboard's text field, the maximum number of iterates to be considered should be updated accordingly. Clicking the "Reset" button should return the region of the plane drawn to that between $-2$ and $2$ on the real axis, and $-2i$ and $2i$ on the imaginary axis.

Check out the video below to see what this program should look like when executed...