Mandelbrot zoom problem
in
Programming Questions
•
1 year ago
I changed Daniel Shiffman's learning code for Mandelbrot a little bit to be able to zoom in where I click. After a little more than a dozen clicks it fails (no error, the image fails). I'm wondering if there is a better way to do this? I'm not sure why exactly it fails:
- float xmin = -2.25;
- float ymin = -1.5;
- float wh = 3;
- int maxIterations = 255;
- void setup() {
- size(400, 400, P2D);
- }
- void draw() {
- loadPixels();
- float xmax = xmin+wh;
- float ymax = ymin+wh;
- float dx = (xmax-xmin)/width;
- float dy = (ymax-ymin)/height;
- float cx = xmin;
- for (int i = 0; i < width; i++) {
- float cy = ymin;
- for (int j = 0; j < height; j++) {
- float zx = 0;
- float zy = 0;
- int numIterations = 0;
- while (numIterations < maxIterations) {
- float zzx = zx*zx;
- float zzy = zy*zy;
- float twoComplex = 2.0*zx*zy;
- zx = zzx-zzy+cx;
- zy = twoComplex+cy;
- if (zx+zy > 16.0) break;
- numIterations++;
- }
- if (numIterations == maxIterations) pixels[i+j*width] = 0;
- else pixels[i+j*width] = numIterations+(numIterations<<8)+(numIterations<<16);
- cy += dy;
- }
- cx += dx;
- }
- updatePixels();
- noLoop();
- }
- void mousePressed() {
- wh /= 2;
- float xmax = xmin+wh;
- float ymax = ymin+wh;
- float dx = (xmax-xmin)/width;
- float dy = (ymax-ymin)/height;
- xmin += dx*mouseX;
- ymin += dy*mouseY;
- loop();
- }
1