見出し画像

Generative Art #109

Code

ArrayList<Form> form = new ArrayList<Form>();

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

void draw() {
  background(0);
  strokeWeight(0.5);
  divideRect(0, 0, width, height, 9);
  circles();
  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();
  fill(getCol());
  if (n == 0) {
    if (random(1) > 0.4) {
      rect(x, y, w, h);
    } else {
      form.add(new Form(x, y, w, h));
    }
  }
  n--;
  if (n>=0) {
    if (w>=h) {
      float randomW = w/2;
      divideRect(x, y, randomW, h, n);
      divideRect(x+randomW, y, w-randomW, h, n);
    }
    if (w<h) {
      float randomH = h/2;
      divideRect(x, y, w, randomH, n);
      divideRect(x, y+randomH, w, h-randomH, n);
    }
  }
}

void circles() {
  for (int i = 0; i < 20; i ++) {
    float d = random(50, 250);
    float posX = random(width);
    float posY = random(height);
    noStroke();
    if (random(1) > 0.66) {
      fill(getCol());
      circle(posX, posY, d);
    } else if (random(1) > 0.33) {
      fill(getCol());
      circle(posX, posY, d);
      fill(getCol());
      circle(posX, posY, d*0.9);
    } else {
      noFill();
      strokeWeight(d * 0.05);
      stroke(getCol());
      circle(posX, posY, d*0.9);
    }
  }
}

int[] colors = {#540D6E, #EE4266, #FFD23F, #3BCEAC, #0EAD69, #F59928};
int getCol() {
  return colors[(int)random(colors.length)];
}

void mousePressed() {
  redraw();
  form.clear();
}

void keyPressed() {
  if (key == 's')saveFrame("####.png");
}

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

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 = 10;
  }

  void show() {
    noStroke();
    fill(fillCol);
    rect(x, y, w, h);
  }
}

タイルに溶け込む円。

頭の中にある表現を実装できて満足。

こうみえても構造は前回と変わらず。

カレーと肉じゃがぐらいの違いみたいなものだ。

この形なら再帰使わなくてもいいけど、使わなくても使うのが再帰ッカー。

Happy coding!

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