Search code examples
javafractals

Mandelbrot Set Fractal Code


I use the following Complex class file for Complex variables.

The java code below is an example of iterations calculator for Mandelbrot Set.

public int iterations(Complex no) {
    Complex z = no;
    int iterations = 0;
    while (z.modulusSquared() < 4 && iter <= MAX_ITERATIONS) {
        z = z.square();
        z = z.add(y);
        iter++;
    }
    return iter;
}

Thanks in advance!


Solution

  • I think in the function squared you need to use the absolute value:

     public Complex square() {
        double newreal;
        double newimaginary;
    
        newreal = ((real * real) - (imaginary * imaginary));
        newimaginary = 2 * abs(imaginary * real);
    
        return new Complex(newreal, newimaginary);
    }
    

    enter image description here

    Now, to square a complex number, I expand this equation: (Zx + Zyi)2 = Zx × Zx + Zx × Zy +Zx × Zy - Zy×Zy = Zx2-Zy2 + 2(Zx×Zy) The real part is Zx2-Zy2. It is quicker to multiply them together (the ZxZx part) than use a function for raising a number to another. The imaginary part is 2(Zx×Zy). It is quicker to set a variable n = ZxZy then set n = n + n to avoid multiplying by two (adding is quicker than multiplying). Zy is a floating point number so I cannot do a bit shift left to multiply by two. Now the part that is different to the Mandelbrot set is this: Zy=Math.abs(Zx*Zy);

    [¹]http://spanishplus.tripod.com/maths/FractalBurningShip.htm