This image is an approximation of the Mandelbrot set mapped to pixels in a bitmap.

To create the image we first create a Cartesian plane with the x-axis running from -1.5 to 2.0 and the y-axis running from -i to i and then
map the coordinate plan to a 300 x 300 pixel grid. This means that the upper and left most pixel represents the complex number
(-1.5, i) and the lower and right most pixel represents (2, -i).

We then take each complex number that is mapped to a pixel and test it to see if we think it might be a member of the Mandelbrot
Set. If we guess that the number is in the set we paint it white. If we believe the number does not belong in the set we paint it
black. The test we use is to take the number square it and then add the original number back to the result and square it again. This is repeated
for a set number of times or until one of the coefficients exceeds a certain boundary.

The algorithm looks like this.

Use 'a' to represent the real part of the complex number and 'b' to represent the coefficient of the imaginary part.

Step 1. Make a copy of (a, b) and call it (c, d)

Step 2. Square (c, d) and store the result back in (c, d)

Step 3. Test that -1000 < c < 1000 and that -1000 < d < 1000.
If the test fails we assume that the number is not in the set and quit. Otherwise continue.

Step 4. Add (a, b) to (c, d) and store the result in (c, d).

Step 5. Test that we have performed the steps 1000 times. If the test passes we quit and assume the number is a part of the set.
Otherwise we repeat the steps starting at step 2.

The algorithm expressed in Java code.

boolean isInMandelbrotSet(double real, double imaginary)

{

double originalReal = real;

double originalImaginary = imaginary;

for (int count = 0; count < 1000; ++count)

{

return true;

}
double originalImaginary = imaginary;

for (int count = 0; count < 1000; ++count)

{

// this is just binomial multiplication with taking into account that i * i = -1

double tempReal = (real * real) - (imaginary * imaginary);

imaginary = 2 * real * imaginary;

// add the original back to the result.

real = tempReal + originalReal;

imaginary = imaginary + originalImaginary;

// test to see if we are still within the boundaries that we have set.

if (real < -1000 || real > 1000 || imaginary < -1000 || imaginary > 1000)

{

}
double tempReal = (real * real) - (imaginary * imaginary);

imaginary = 2 * real * imaginary;

// add the original back to the result.

real = tempReal + originalReal;

imaginary = imaginary + originalImaginary;

// test to see if we are still within the boundaries that we have set.

if (real < -1000 || real > 1000 || imaginary < -1000 || imaginary > 1000)

{

return false;

}
return true;

Fractal Cafe home

Open School Games home