見出し画像

Generative Art #148

Code

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

void draw() {
 int c = 10;
 background(getCol());
 lineRec(width*0.25, height*0.25, c);
 lineRec(width*0.75, height*0.25, c);
 lineRec(width*0.75, height*0.75, c);
 lineRec(width*0.25, height*0.75, c);
 lineRec(width*0.5, height*0.5, c);

 divideRect(0, 0, width, height, 4);

 //saveFrame("output/####.png");
 //if (frameCount == 60) {
 //  exit();
 //}
}

void lineRec(float x, float y, int n) {
 float l = random(10, 400);
 float shift = random(l);
 float angle = QUARTER_PI * int(random(8));
 float d = random(10, 40);
 color c = getCol();

 push();
 translate(x, y);
 rotate(angle);

 stroke(c);
 strokeWeight(1.5);
 line(0, 0, l, 0);

 noStroke();
 fill(c);
 circle(0, 0, d);

 if (random(1) < 0.2) {
   stroke(c);
   strokeWeight(0.5);
   noFill();
   circle(0, 0, d * random(1, 20));
 }

 n--;

 if (n >= 0) {
   translate(shift, 0);
   lineRec(0, 0, n);
 }

 pop();
}

void divideRect(float x, float y, float w, float h, int n) {
 if (n == 0) {
   myRect(x, y, w, h);
 }
 n--;
 if (n >= 0) {
   float newW = random(w * 0.1, w * 0.9);
   float newH = random(h * 0.1, h * 0.9);
   divideRect(x, y, newW, newH, n);
   divideRect(x+newW, y, w-newW, newH, n);
   divideRect(x, y+newH, newW, h-newH, n);
   divideRect(x+newW, y+newH, w-newW, h-newH, n);
 }
}

void myRect(float x, float y, float w, float h) {
 int c = int(random(10, 100) * (w + h));
 stroke(getCol());
 strokeWeight(0.4);
 for (int i = 0; i < c; i ++) {
   float wr = random(w);
   float rh = random(h);
   point(x+wr, y+rh);
 }
}

void mousePressed() {
 redraw();
}

int[] colors = {#541388, #d90368, #f1e9da, #2e294e, #ffd400, #6032CE};
int getCol() {
 return colors[(int)random(colors.length)];
}

再帰を使って円と線を配置。その上から矩形を再帰的に分割したフラクタル図形を描画。

初めの取っ掛かりはこんなのからスタートしました。

この時点でかっこいいから、いじりすぎたのかもしれないと今思いました。

それと、いつもはsキーで画像保存するようにしていたんですけど

saveFrame("output/####.png");
if (frameCount == 60) {
  exit();
}

こんな感じで画像をたくさん作ってしまってそこから気に入ったモノを選んだ方が効率的かなと思いました。
特に今回みたいな一回の描画に時間がかかるモノはコーヒーを淹れている間にやってくれるわけですし。

Happy coding!!

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