Search code examples
c++fractals

Mandelbrot Set not displaying at center


I think the problem is how I'm converting the Cartesian coordinate into a Complex number but I have now idea how. Can you please explain how should I convert? Here's what I tried though:

double c_Im = (y + (maxIm - minIm)) / height;
double c_Re = (x + (maxRe - minRe)) / width;

Code:

float minRe = -2.0;
float maxRe = 2.0;
double minIm = -2.0;
double maxIm = 2.0;

for (size_t y = 0; y < height; y++)
    {
        double c_Im = (y + (maxIm - minIm)) / height;

        for (size_t x = 0; x < width; x++)
        {
            double c_Re = (x + (maxRe - minRe)) / width;
            float dx = 0, dy = 0;
            int z = 0;
            while (dx * dx + dy * dy < 4 && z < maxIterator)
            {
                float temp = (dx * dx - dy * dy) + c_Re;
                dy = 2 * dx * dy + c_Im;
                dx = temp;
                z++;
            }
            image.setPixel(x, y, Color(z % 255, z % 255, z % 255));

        }
    }

not_expected_output.jpg not expected output


Solution

  • I think the formula you're looking for is this:

    double c_Im = y * (maxIm - minIm) / height + minIm;

    double c_Re = x * (maxRe - minRe) / width + minRe;

    This is derived from the map formula: Y=(X-A)*(D-C)/(B-A)+C