4
votes

I have successfully implemented the mandelbrot set as described in the wikipedia article, but I do not know how to zoom into a specific section. This is the code I am using:

+(void)createSetWithWidth:(int)width Height:(int)height Thing:(void(^)(int, int, int, int))thing
{   
    for (int i = 0; i < height; ++i)
    for (int j = 0; j < width; ++j)
    {
        double x0 = ((4.0f * (i - (height / 2))) / (height)) - 0.0f;
        double y0 = ((4.0f * (j - (width / 2))) / (width)) + 0.0f;
        double x = 0.0f;
        double y = 0.0f;

        int iteration = 0;
        int max_iteration = 15;

        while ((((x * x) + (y * y)) <= 4.0f) && (iteration < max_iteration))
        {
            double xtemp = ((x * x) - (y * y)) + x0;
            y = ((2.0f * x) * y) + y0;
            x = xtemp;
            iteration += 1;
        }

        thing(j, i, iteration, max_iteration);
    }
}

It was my understanding that x0 should be in the range -2.5 - 1 and y0 should be in the range -1 - 1, and that reducing that number would zoom, but that didnt really work at all. How can I zoom?

2
also, the entire set is contained within -2 < x < 2, -2 < y < 2. not sure what you mean with the numbers you gave.jcomeau_ictx
For the "never-ending zooms" I believe some property of the fractal algorithm itself is used.user166390

2 Answers

2
votes

first off, with a max_iteration of 15, you're not going to see much detail. mine has 1000 iterations per point as a baseline, and can go to about 8000 iterations before it really gets too slow to wait for.

this might help: http://jc.unternet.net/src/java/com/jcomeau/Mandelbrot.java

this too: http://www.wikihow.com/Plot-the-Mandelbrot-Set-By-Hand

5
votes

Suppose the center is the (cx, cy) and the length you want to display is (lx, ly), you can use the following scaling formula:

x0 = cx + (i/width - 0.5)*lx;

y0 = cy + (j/width - 0.5)*ly;

What it does is to first scale down the pixel to the unit interval (0 <= i/width < 1), then shift the center (-0.5 <= i/width-0.5 < 0.5), scale up to your desired dimension (-0.5*lx <= (i/width-0.5)*lx < 0.5*lx). Finally, shift it to the center you given.