5

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?

Nippysaurus
  • 20,110
  • 21
  • 77
  • 129
  • 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 Dec 10 '10 at 04:06
  • For the "never-ending zooms" I believe some property of the fractal algorithm itself is used. –  Dec 10 '10 at 04:21

2 Answers2

5

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.

unsym
  • 2,080
  • 1
  • 20
  • 19
  • Thank you! Found your response to be useful as I also came across the need to scale the Mandelbrot set. However may I ask what does x0 and y0 refer to in this case. I am unfamiliar with the C language – Robert Garza-Altuna May 09 '20 at 07:48
2

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

jcomeau_ictx
  • 37,688
  • 6
  • 92
  • 107
  • I was creating a large set and wanted it to go quicker so put it down to 15 ... it was 1000 previously. – Nippysaurus Dec 10 '10 at 03:54
  • The more you zoom, the more iterations you'll need to see differences between points. Too few iterations and you'll just see all one colour. At higher zooms you'll need more iterations to show the details. – Graham Perks Dec 10 '10 at 05:31