1

I managed to create the Mandelbrot set in processing but am struggling to implement a zoom. Here is my code:

float minvalX = -1.5;
float minvalY = -1.5;
float maxvalX = 1.5;
float maxvalY = 1.5;
float angle = 0;
float di,dj;
int xPixel,yPixel;





void setup(){
  size(500,500);
  pixelDensity(1);
  colorMode(HSB, 360);

}


void draw() {
  
  scale(zoom);
  float maxLoops = 100;
  loadPixels();

  
  float equationOneOriginal; 
  float equationTwoOriginal; 
  
  
  for (xPixel = 0; xPixel < width ; xPixel++) {
    for (yPixel = 0; yPixel < height ; yPixel++) {
      
      float a = map(xPixel+di, 0,width, minvalX, maxvalX);
      float b = map(yPixel+dj, 0,height, minvalY, maxvalY);
      
      equationOneOriginal = a;
      equationTwoOriginal = b;
      
      float n = 1;
      
      while (n < maxLoops) {
        
        float equationOne  = a*a - b*b; //First part of the equation 
        float equationTwo  = 2 * a * b; //Second part of the equation
        
        a = equationOne  + equationOneOriginal;
        b = equationTwo  + equationTwoOriginal;
        
        if (abs(a+b) > 16) {
          break;
        }
        
        n++;
      }
      

      
      if (n == maxLoops) {
      
        pixels[xPixel+yPixel*width] = color(0);
      
      }
      else {
        pixels[xPixel+yPixel*width] = color(n-(int)(n/360)*n, 360, (int)map(n*6, 1, maxLoops, 0, 360));
      
      }
      
    }
  }
  
  updatePixels();
  
}

void mousePressed()
{
  
  if (mouseButton == LEFT) {
    di = di + mouseX - int(width/2);
    dj = dj + mouseY - int(height/2);
    
    minvalX += 0.1;
    maxvalX -= 0.1;
    
    minvalY += 0.1;
    maxvalY -= 0.1;
    
  }
  
}



It zooms into where my mouse is but eventually it goes towards the middle and gets stuck there. I know it is to do with minvalX, maxvalX, minvalY and maxvalY but I don't know what to do to these values to get it to always zoom towards my mouse.

Pathfinder
  • 23
  • 4
  • I believe [this answer](https://stackoverflow.com/a/4405592/5844572) may be of help. – laancelot Oct 08 '20 at 02:13
  • I'm not sure whether it's because I'm not familiar with Objective C but I was not able to get the code in this answer working with my code. Could you expand on how this answer could help? – Pathfinder Oct 08 '20 at 09:00
  • You'll not zoom far by using the inadequate `float`. – Weather Vane Oct 08 '20 at 13:32
  • 1
    This, and also he's only iterating 10 times while some people go for thousands as seen in the other answer in the link provided. But that's not the issue he's gunning for: when he zooms, the sketch tends to zoom a little bit toward the center every time, and the further he zooms the worse it becomes, making the sketch irrelevant after a while. Of course, the `minvalX += 0.1;` bunch of lines cannot work after some of them pass zero, but I'm not good at math enough to provide a working alternative. – laancelot Oct 08 '20 at 16:09
  • 1
    I suppose that it should change in relation with the zoom level, but I haven't crunched the numbers in any relevant way to accomplish this. – laancelot Oct 08 '20 at 16:11

0 Answers0