1
\$\begingroup\$

I have used my limited knowledge of processing and made a sketch to generate the Mandelbrot set and various Julia sets. I've tried to make it efficient as possible. It's also interactive, and keyPressed() should explain that sufficiently.

Here is the code:

int mi = 255;
float r = 0.5, a = 0;
float[] bbox = new float[]{-0.5, 0, 2};
float[] c = new float[]{-0.8, 0.15};
boolean rot = false, mode = true;

void setup() {
  size(720, 720);
  display();
}

void draw() {
  if (rot) {c[0] = r*cos(a*PI); c[1] = r*sin(a*PI); a += 0.01; r += 0.001; display();}
}

void display() {
  loadPixels();
  float q, za, zb, zas, zbs, ta;
  int i = 0, x, y;
  for (x = 0; x < width; x++) {
    for (y = 0; y < width; y++) {
      za = map(x, 0, width, bbox[0]-bbox[2], bbox[0]+bbox[2]);
      zb = map(y, 0, width, bbox[1]-bbox[2], bbox[1]+bbox[2]);
      q = pow((za-0.25),2)+zb*zb;
      if (mode) {c[0] = za; c[1] = zb;}
      if (!mode || q*(q+(za-1/4)) >= zb*zb/4) {
        zas = za*za; zbs = zb*zb;
        for (i = 0; i < mi; i++) {
          ta = zas-zbs+c[0];
          zb = 2*za*zb+c[1];
          za = ta;
          zas = za*za; zbs = zb*zb;
          if (zas+zbs > 4) {
            break;
          }
        }
      }
      pixels[x+y*width] = color(map(sqrt(i%mi), 0, sqrt(mi), 0, 255));
    }
  }
  updatePixels();
}

void keyPressed() {
  //Zoom window
  if (key == 'e') {bbox[2] /= 2; display();} //Zoom in
  if (key == 'q') {bbox[2] *= 2; display();} //Zoom out

  //Pan window
  if (key == 'w') {bbox[1] -= bbox[2]/5; display();} //Pan up
  if (key == 'a') {bbox[0] -= bbox[2]/5; display();} //Pan left
  if (key == 's') {bbox[1] += bbox[2]/5; display();} //Pan down
  if (key == 'd') {bbox[0] += bbox[2]/5; display();} //Pan right

  //Resetting
  if (key == 'c') {c = new float[]{-0.8, 0.15}; display();} //Reset c value
  if (key == 'x') {if (mode) {bbox = new float[]{-0.5, 0, 2};} else {bbox = new float[]{0, 0, 2};}; display();} //Reset view

  //Changing mode
  if (key == 'z') {mode = !mode; if (mode) {bbox = new float[]{-0.5, 0, 2};} else {bbox = new float[]{0, 0, 2};}; display();} //Toggle mandelbrot/julia set mode

  //Animations
  if (key == 'r' && !mode) {rot = !rot; display();} //Toggle julia set rotating animation

  //Offset c value in f(z) = z^2 + c
  if (key == 't') {c[0] -= 0.01; display();} //Offset up
  if (key == 'f') {c[1] -= 0.01; display();} //Offset left
  if (key == 'g') {c[0] += 0.01; display();} //Offset down
  if (key == 'h') {c[1] += 0.01; display();} //Offset right

  // Change iteration number
  if (key == 'v') {mi = constrain(mi-8, 7, 1023); display();} //Decrease iteration number
  if (key == 'b') {mi = constrain(mi+8, 7, 1023); display();} //Increase iteration number
}

The reason that there is so much redundancy of display(); is that if I put it in the draw loop, it has a tendency to miss key presses. Also, i apologize for the cryptic variable names, however, most should be self explanatory.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.