日常の進捗

主に自分のための,行為とその習慣化の記録

Mod:Coding Challenge #22: Julia Set in Processing

昨日のマンデルブロ集合とつながるジュリア集合。動画で書かれているコード写しながら動画内で参照されているリファレンスを読んだ。

Julia set fractal Julia set - Wikipedia

マンデルブロ集合のコードを改変して一定の数値を与えると形が変わる、その加算する定数を変化させるとアニメーションする。

コード

float theta = 0;

void setup() {
  size(960, 540);
  colorMode(HSB, 360, 100, 100, 100);
}

void draw() {
  float cx = map(sin(frameCount*0.003), -1, 1, 3.3, 1.5);
  float cy = map(cos(frameCount*0.0057), 0, 1, 0.1, 3.3);

  float ca = cos(theta * cx);
  float cb = sin(theta * cy);

  //float ca = map(mouseX, 0, width, -1, 1);
  //float cb = map(mouseY, 0, height, -1, 1);
  theta += 0.01;
  background(0, 0, 100);

  float w = 4.4;
  float h = ( w * height) / width;

  float xMin = -w / 2;
  float yMin = -h / 2;

  loadPixels();
  int maxIterations = 100;
  float xMax = xMin + w;
  float yMax = yMin + h;

  float dx = (xMax - xMin) / width;
  float dy = (yMax - yMin) / height;

  float y = yMin;
  for (int j = 0; j < height; j++) {
    float x = xMin;
    for (int i = 0; i < width; i++) {
      float a = x;
      float b = y;
      int n = 0;
      while (n < maxIterations) {
        float aa = a * a;
        float bb = b * b;
        if (aa + bb > 4.0) {
          break;
        }
        float twoab = 2 * a * b;
        a = aa - bb + ca;
        b = twoab + cb;
        n++;
      }

      if ( n == maxIterations) {
        pixels[i+j*width] = color(0, 0, 0);
      } else {
        float brightness = sqrt(float(n) / maxIterations);
        brightness = map(brightness, 0, 1, 0, 100)%100;
        pixels[i+j*width] = color(0, 0, brightness);
      }
      x += dx;
    }
    y += dy;
  }

  updatePixels();
}

リファレンス