2

I have a general question with the Mandelbrot set "zoom" view and the math pertaining to it. I have implemented the mandelbrot set for the 256 X 256 window size with values

  // ImageWidth = ImageHeight = 256;

  double MinRe = -2.0;
  double MaxRe = 1.0;
  double MinIm = -1.2;
  double MaxIm = 1.8;

  ComputeMandelbrot();

Next, I select a region of square and these are the coordinates for the upper left most tip (76,55), and rightmost bottom tip (116, 99) (square of side 44 is chosen)

so , I choose x2 = x1 + 44 ; y2 = y1 + 44;

How do I translate these new coordinates to the complex plane ? and how would the new real and imaginary values change in order to compute it for the new set of values ?

This is what I have tried so far..

double Re_factor = (MaxRe-MinRe)/(ImageWidth-1);
double Im_factor = (MaxIm-MinIm)/(ImageHeight-1);

double newMinRe = MinRe + (Re_factor* x1);
double newMaxRe = MaxRe + (Re_factor* x2);
double newMinIm = MinIm + (Im_factor* y1);
double newMaxIm = MaxIm + (Im_factor* y2);

// and then I compute c - real and c- imag values

  for(unsigned y=0; y<ImageHeight; ++y) 
{ 
  double c_im = newMaxIm - y*Im_factor;
  for(unsigned x=0; x<ImageWidth; ++x)
    {
      double c_re = newMinRe + x*Re_factor;

      // ComputeMandelbrot();

     }

 }

I am having a hard time figuring out the math, and also with regards to generating a 'zoom' view and any help is appreciated !!

genpfault
  • 51,148
  • 11
  • 85
  • 139
Legolas
  • 12,145
  • 12
  • 79
  • 132

1 Answers1

7

It's a linear scaling. Let's doing it in 1D. You have the screen space (screen coordinates), and the image space (the complex plane, in your case)

  • screen space => [0, 255]
  • image space => [-2, 1]

So to convert a coordinate X from screen space to image space X'

X' = (X / 255) * (1 - (-2)) + (-2)

To make it more generic

  • screen space => [SMin, SMax]
  • image space => [IMin, IMax]

X' = ((X - SMin) / (SMax - SMin)) * (IMax - IMin) + IMin


In your code, you do

double newMinRe = MinRe + (Re_factor* x1);

which is equivalent to what I show. But then you do

double newMaxRe = MaxRe + (Re_factor* x2);

which is not correct, and should be

double newMaxRe = MinRe + (Re_factor* x2);

Same problem in your loop, it should be

for(unsigned y=0; y<ImageHeight; ++y)  { 
  double c_im = MinIm + y*Im_factor;
  for(unsigned x=0; x<ImageWidth; ++x) {
    double c_re = MinRe + x*Re_factor;
    // ComputeMandelbrot();
  }
}

Additional detail for extra-goodness : to sample properly the image space , I suggest this

for(unsigned SX = SMin; x < SMax; ++x) {
  double k = (double(SX + 0.5) - SMin) / (SMax - SMin);
  double IX = (k * (IMax - IMin)) + IMin;
}

The +0.5 term is to sample right in the middle of the pixel...

Monkey
  • 1,838
  • 1
  • 17
  • 24
  • Nope, you think you are doing that in your code, but in reality you do something else ^^ – Monkey Dec 05 '11 at 06:59
  • I get it !! But it seems to be displaying a different region in the mandelbrot set with a different square size. :( Check more code. – Legolas Dec 05 '11 at 07:17
  • You do the same error in your loop, look again... Also, sample in the middle of your pixel, not in the corner ^^ – Monkey Dec 05 '11 at 07:28
  • The `Im_factor` and the `Re_factor` have to calculated again right ? for the new range ? – Legolas Dec 05 '11 at 07:31
  • Not sure about what you mean. Before precomputing stuffs, try the plain thing, *then* precompute factors if you like. Premature optimization is the root of all evils ;) – Monkey Dec 05 '11 at 07:35
  • so yeah, had to recalculate the Im_factor and Re_factor again, and the problem was in the region i selected the square from. But yes, I really thank you for your time. :D – Legolas Dec 05 '11 at 19:55