見出し画像

Generative Art #108

Code

ArrayList<Form> form = new ArrayList<Form>();
int count = 9;
int actRandomSeed = (int)random(10000);

void setup() {
  size(840, 840);
  pixelDensity(2);
}

void draw() {
  randomSeed(actRandomSeed);
  background(0);
  strokeWeight(0.5);
  divideRect(0, 0, width, height, count);
  for (int i = 0; i < form.size(); i ++) {
    Form f = form.get(i);
    f.show();
  }
}

void divideRect(float x, float y, float w, float h, int n) { 
  noStroke();
  stroke(0);
  fill(getCol());
  if (n == 0) {
    if (random(1) > 0.5) {
      rect(x, y, w, h);
    } else {
      form.add(new Form(x, y, w, h));
    }
  }
  n--;
  if (n>=0) {
    if (w>=h) {
      float randomW = random(w*0.1, w*0.9);
      divideRect(x, y, randomW, h, n);
      divideRect(x+randomW, y, w-randomW, h, n);
    }
    if (w<h) {
      float randomH = random(h*0.1, h*0.9);
      divideRect(x, y, w, randomH, n);
      divideRect(x, y+randomH, w, h-randomH, n);
    }
  }
}

int[] colors = {#9B26EA, #F0386B, #FF5376, #E2C290, #F8C0C8, #F7A137};
int getCol() {
  return colors[(int)random(colors.length)];
}

void mousePressed() {
  form.clear();
  actRandomSeed = (int)random(1000000);
}

void keyPressed() {
  if (key == 's')saveFrame("####.png");
  if (keyCode == UP)count ++;
  if (keyCode == DOWN)count --;
}

//----------------------------------------------------------------------

class Form {
  float x, y, w, h;
  color fillCol;
  float shift;
  Form(float x, float y, float w, float h) {
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    fillCol = getCol();
    shift = (w+h)*0.05;
  }

  void show() {
    noStroke();
    stroke(0);
    fill(fillCol);
    rect(x, y, w, h);
    fill(50, 180);
    rect(x, y, w, h);
    fill(fillCol);
    rect(x-shift, y-shift, w, h);
  }
}


今度は一定の確率でArrayListに入れて、ランダムに選ばれた四角形が浮いているような表現を作った。

気持ちいいフラクタルに気持ちいい立体感。

気付けば自然にArrayListを使えるようになってんだなぁと実感。Nature of codeで初めて見た時は何書いているのかさっぱりだったなー。

ありがとうShiffman先生、おめでとう人類。

Happy coding!

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