見出し画像

Generative Art #143

Code

int c = 5;

void setup() {
 size(900, 900);
 pixelDensity(2);
 noLoop();
 colorMode(HSB, 360, 100, 100, 100);
 rectMode(CENTER);
 noStroke();
}

void draw() {
 noStroke();
 background(#ffffff);
 squareRec(width/2, height/2, width, c);
 powder();
}

void squareRec(float x, float y, float s, int n) {
 float hs = s/2;

 fill(random(360), 90, 95);
 square(x, y, s);

 fill(random(360), 90, 95);
 pattern(x, y, s);

 beginShape();
 vertex(x-hs, y-hs);
 vertex(x-hs, y+hs);
 vertex(x+hs, y+hs);
 vertex(x+hs, y-hs);

 beginContour();
 for (float a = 0; a < TAU; a += TAU/360) {
   vertex(x + s/2 * cos(a), y + s/2 * sin(a));
 }
 endContour();

 fill(random(360), 30, 95);
 endShape();

 n--;
 if (n >= 0) {
   float probability = map(n, 0, c-1, 0.8, 0);
   float qs = hs/2;

   if (random(1) > probability) {
     squareRec(x -qs, y - qs, hs, n);
     squareRec(x + qs, y - qs, hs, n);
     squareRec(x - qs, y + qs, hs, n);
     squareRec(x + qs, y + qs, hs, n);
   }
 }
}

void pattern(float x, float y, float s) {
 int p = int(random(2));
 float hs = s/2;

 switch(p) {
 case 0:
   pushMatrix();
   translate(x - hs, y - hs);
   float count = int(random(3, 7));
   float ss = s/count;
   float sss = ss*random(0.5, 1);
   for (float j = 0; j < count; j++) {
     for (float i =0; i < count; i++) {
       square(i * ss + ss/2, j * ss + ss/2, sss);
     }
   }
   popMatrix();
   break;

 case 1:
   for (int i = 0; i < 30; i ++) {
     float d = random(s/10);
     circle(random(x - hs + d, x + hs - d), random(y - hs + d, y + hs - d), d);
   }
   break;
 }
}

void mousePressed() {
 redraw();
}

void powder() {
for (int i = 0; i < 300000; i ++) {
  fill(random(300, 360), 30);
  circle(random(width), random(height), 0.6);
}
}

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

構造は正方形の再帰です。

こんな模様に

こんな感じで円形にくり抜いた四角形を上から描画しています。

それでこんな風になります。

久しぶりのカラーモードHSB使いました。色相だけをランダムにする形。

存在はだけは知っていたけど使ったことのないswitch文を初めて使ってみました。

Happy Coding!



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