見出し画像

Generative Art #156

Code

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

void draw() {
 background(#ffffff);
 tile(0, 0, width);
 saveFrame("output/####.png");
 if (frameCount == 100)exit();
}

void tile(float x_, float y_, float w_) {
 int tileCount = (int)random(2, 6);
 float tileWidth = w_/tileCount;
 color col = getCol();

 for (float x = x_; x < x_ + w_ -1; x += tileWidth) {
   for (float y = y_; y < y_ + w_ - 1; y += tileWidth) {
     if (random(1) < 0.5 && tileWidth > 50) {
       tile(x, y, tileWidth);
     } else {
       fill(col);
       stroke(#E5E5E5);
       strokeWeight(0.7);
       rect(x, y, tileWidth, tileWidth);
       randomShape(x+tileWidth/2, y+tileWidth/2, tileWidth);
     }
   }
 }
}

void randomShape(float x, float y, float s) {
 int rnd = (int)random(7);
 float hs = s/2;
 color col = getCol();
 
 push();
 translate(x, y);
 fill(col);
 if (rnd == 0) {
   circle(0, 0, s);
 }
 if (rnd == 1) {
   rotate((int)random(4) * HALF_PI);
   arc(-hs, -hs, s*2, s*2, 0, HALF_PI);
 }
 if (rnd == 2) {
   rotate((int)random(4) * HALF_PI);
   beginShape();
   vertex(hs, hs);
   vertex(-hs, hs);
   vertex(-hs, -hs);
   endShape(CLOSE);
 }
 if (rnd == 3) {
   square(-s*0.35, -s*0.35, s*0.7);
 }
 if (rnd == 4) {
   rotate((int)random(4) * HALF_PI);
   arc(0, -hs, s, s, 0, PI);
 }
 pop();
}

void mousePressed() {
 redraw();
}

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

int[] colors = {#560D7F, #A134BE, #FFDF01, #EAA21E, #FAC100, #2825B9, #464646, #E5E5E5};
int getCol() {
 return colors[(int)random(colors.length)];
}

僕が好んで使っているこの再帰を使って正方形を分割するフラクタルですが、また少し進化しました。

こんな感じで分割する数を簡単に変えられるようにしました。

void setup() {
 size(600, 600);
 noLoop();
}

void draw() {
 background(0);
 tile(0, 0, width-1);
}

void tile(float x_, float y_, float w_) {
 int tileCount = (int)random(2, 6);
 float tileWidth = w_/tileCount;
 float min = 20;

 noFill();
 stroke(255);
 for (float x = x_; x < x_ + w_ -1; x += tileWidth) {
   for (float y = y_; y < y_ + w_ - 1; y += tileWidth) {
     if (random(1) < 0.7 && tileWidth > min) {
       tile(x, y, tileWidth);
     } else {
       rect(x, y, tileWidth, tileWidth);
     }
   }
 }
}

void mousePressed() {
 redraw();
}

tileCountをランダムにすることで分割する数を変えています。
tileCount = 2の場合は2×2で4つに分割します。


Happy coding!!



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