見出し画像

ProcessingによるGenerative art作品#3

フラクタルの木です。

コード

int _numChildren = 8;
int _maxLevels = 6;
Branch _trunk;

// =======================

void setup() {
  size(1000, 750);
  pixelDensity(2);
  colorMode(HSB, 360, 100, 100, 100);
  smooth();
  noLoop();
}
void draw() {
  background(58, 2, 97);
  newTree();
}

void mousePressed() {
  redraw();
}

void newTree() {
  _trunk = new Branch(1, 0, width/2, height-30);
  _trunk.drawMe();
}

// =======================

class Branch {
  float level, index;  
  float x, y;
  float endx, endy;
  float midx, midy;

  Branch [] children = new Branch[0]; 

  Branch(float lev, float ind, float ex, float why) {
    level = lev; 
    index = ind;
    updateMe(ex, why);

    if (level < _maxLevels) {
      children = new Branch[_numChildren];
      for (int x=0; x<_numChildren; x++) {
        children[x] = new Branch(level+1, x, midx, midy);
      }
    }
  }

  void updateMe(float ex, float why) {
    x = ex;
    y = why;
    endx = x + (level * (random(80) - 40));
    endy = y + (level * (random(-60) ));
    //中間点
    midx = (x + endx)/2 + (random(80)-40);
    midy = (y + endy)/2 + (random(80)-40);
  }

  void drawMe() { 
    strokeWeight(_maxLevels - level + 1);
    stroke(74, 60, 23, 30);
    noFill();
    beginShape();
    curveVertex(x, y);
    curveVertex(x, y);
    curveVertex(midx, midy);
    curveVertex(endx, endy);
    curveVertex(endx, endy);
    endShape();
    noStroke();
    fill(13, 89, 95, 10);
    for (int i=10; i<=30;i+=2) {
      ellipse(endx, endy, i, i);
    }

    for (int i=0; i<children.length; i++) {  
      children[i].drawMe();
    }
  }
}

単純なフラクタルですが、ここからいろんなものに派生できそうでなんだかワクワクします。
枝に少しカーブを加えて枝っぽくして見ましたが、いまいち面白みがないですね。


書籍「ジェネラティブ・アート Processingによる実践ガイド」を参考に作成しました。今の所は作品はほとんどこの本を参考にして作っています。



応援してくださる方!いつでもサポート受け付けてます!