日常の進捗

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

Mod:Coding Challenge #28: Metaballs

f:id:takawo:20170929120346g:plain

いわゆるメタボール。後半Computer Visionを使うパートになっていくんだけどいまいち理解できてないので、CVのチュートリアルを明日以降やって戻ってきたいと思う。OpenProcessingに載せてみたが重くて1fpsくらいでしか動かなかった。

コード

int num = 8;
ArrayList<Blob> blobs = new ArrayList<Blob>();

// setup関数 : 初回1度だけ実行される
void setup() {
  size(960, 540, FX2D); // ウィンドウサイズを960px,540pxに
  colorMode(HSB, 360, 100, 100); // HSBでの色指定にする
  for (int i = 0; i< num; i++) {
    blobs.add(new Blob(random(width), random(height)));
  }
}

// draw関数 : setup関数実行後繰り返し実行される
void draw() {
  loadPixels();
  for (int y = 0; y < height; y += 1) {
    for (int x = 0; x < width; x += 1) {
      int sum = 0;
      for (Blob b : blobs) {
        float dist = dist(x, y, b.pos.x, b.pos.y);
        sum += 200 * b.radius / dist;
      }
      pixels[x + y * width] = color(frameCount%360, 80, sum/8f);
    }
  }
  updatePixels();
  for (Blob b : blobs) {
    b.update();
    b.draw();
  }
}

class Blob {
  PVector pos;
  PVector vel;
  float radius = 100;
  Blob(float x, float y) {
    pos = new PVector(x, y);
    vel = PVector.random2D().mult(random(3, 6));
  }
  void update() {
    pos.add(vel);
    if (pos.x < 0 || pos.x > width) {
      vel.x *= -1;
    }
    if (pos.y < 0 || pos.y > height) {
      vel.y *= -1;
    }
  }

  void draw() {
  }
}

リファレンス