0

I'm still relatively new to Java. I've been working on a program to display the mandelbrot set. My current code generates an image that is close, but not quite the mandelbrot set. This is my generation code:

private void generateMap () {
    // scale, ITERATIONS, map, and SIZE are class variables
    // cR and cI are the actual coordinates in the set being used
    double cR = -2*scale;
    double cI = -2*scale;
    // a and b step through the array used to store the drawing
    // and control when the loop exits
    for (int a = 0; a < SIZE.width; a++) 
    {
        for (int b = 0; b < SIZE.height; b++) 
        {
            double xR = 0;
            double xI = 0;
            int iter = 0;
            while (iter < ITERATIONS) 
            {
                xR = (xR*xR-xI*xI) + cR;
                xI = (2*xR*xI) + cI;
                if (xR*xR+xI*xI > 4) {
                    map[a][b] = iter;
                    iter = ITERATIONS;
                }
                iter++;
            }
            cI += INCREMENT*scale;
        }
        cI = -2*scale;
        cR += INCREMENT*scale;
    }
}

My netbeans project is downloadable from here.

Here's a screenshot of the current output: screenshot

wchargin
  • 15,589
  • 12
  • 71
  • 110
leopardGeckos
  • 91
  • 2
  • 8
  • Not your issue, but your while loop could be rewritten: `for (int iter = 0; iter < ITERATIONS; i++) {xR = (xR*xR-xI*xI) + cR; xI = (2*xR*xI) + cI; if (xR*xR+xI*xI > 4) { map[a][b] = iter; break; } }` – assylias Apr 04 '13 at 23:50
  • 1
    For better help sooner, post an [SSCCE](http://sscce.org/) & a link to images of current & expected output. – Andrew Thompson Apr 05 '13 at 00:18

1 Answers1

0

The new values of xR and xI are not being calculated consistently. xR is calculated based on its previous value while xI is calculated based on xR's new value. Try something like the following or maybe use a complex number class.

double r = xR;
double i = xI;
xR = (r*r-i*i) + cR;
xI = (2*r*i) + cI;
fgb
  • 18,439
  • 2
  • 38
  • 52