見出し画像

Generative Art #137

Code

ArrayList<Form> forms;
Grid grid;
int seed = (int)random(100000);

void setup() {
 size(900, 900, P2D);
 pixelDensity(2);
 smooth(8);
 rectMode(CENTER);
 grid = new Grid(0, 0, width, 100);
}

void draw() {
 randomSeed(seed);
 background(#ffffff);
 
 for (int j = 0; j < 10; j ++) {
   newForm();
   for (int i = 0; i < forms.size(); i ++) {
     Form f = forms.get(i);
     f.show();
   }
 }
 
 powder();
}

void newForm() {
 forms = new ArrayList<Form>();
 boolean useGrid[][ ]= new boolean[grid.cw][grid.cw];
 int empty = grid.cw*grid.cw;
 
 while (empty > 0) {
   
   float c = grid.c;
   int w = int(random(1, grid.cw));
   int x = int(random(grid.cw - w+1));
   int y = int(random(grid.cw - w+1));
   boolean collision = true;
   
   for (int j = 0; j < w; j ++) {
     for (int i = 0; i < w; i ++) {
       if (useGrid[x+i][y+j]) {
         collision = false;
         break;
       }
     }
   }
   
   if (collision) {
     for (int j = 0; j < w; j ++) {
       for (int i = 0; i < w; i ++) {
         useGrid[x+i][y+j] = true;
       }
     }
     forms.add(new Form(grid.x + x * c, grid.y + y * c, w * c));
     empty -= w * w;
   }
   
 }
}

void powder() {
 for (int i = 0; i < 800000; i ++) {
   stroke(random(255), 50);
   strokeWeight(0.6);
   point(random(width), random(height));
 }
}

void mousePressed() {
 redraw();
 seed = (int)random(100000);
}

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

//int[] colors = {#ee6352, #59cd90, #3fa7d6, #fac05e, #f79d84};
int[] colors = {#0A2463, #000000, #FB3640, #247BA0, #E2E2E2};
int getCol() {
 return colors[(int)random(colors.length)];
}

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

class Form {
 float x, y, w;
 float hw, ww;
 color col;
 int count;
 float p;
 float minSize;
 float angle;
 
 Form(float x, float y, float w) {
   this.x = x;
   this.y = y;
   this.w = w;
   hw = w/2;
   count = (int)random(1, 9);
   p = random(1);
   minSize = random(5, 50);
 }
 
 void show() {
   noStroke();
   fill(getCol());
   if (p > 0.5) {
     push();
     translate(x + hw, y + hw);
     squareRec(w, 0, count);
     pop();
   } else if (p > 0.25) {
     square(x+ hw, y+ hw, w);
     fill(getCol());
     graCircle();
   }else {
     for(float i = w; i > 0; i -= w / 10 ){
       angle = int(random(4)) * PI/2;
       graSquare(i);
     }
   }
 }

 void squareRec(float s, float a, int n) {
   float hs = s/2;
   rotate(a);
   noStroke();
   fill(getCol());
   
   beginShape();
   vertex(-hs, -hs);
   vertex(hs, -hs);
   fill(getCol());
   vertex(hs, hs);
   vertex(-hs, hs);
   endShape(CLOSE);
   
   n--;
   
   if (s > minSize) {
     float angle = random(PI/2);
     s = s/(sin(angle) + cos(angle));
     squareRec(s, angle, n);
   }
 }

 void graCircle() {
   float r = w/2;
   
   pushMatrix();
   translate(x + hw, y + hw);
   rotate(random(PI));
   
   beginShape();
   noStroke();
   fill(getCol());
   for (float theta = 0; theta <= TWO_PI; theta += radians(1)) {
     float x = r * cos(theta);
     float y = r * sin(theta);
     
     if (theta > TWO_PI/2) {
       fill(col);
     } 
     vertex(x, y);
   }
   endShape();
   
   popMatrix();
 }
 
 void graSquare(float s){
   float hs = s/2;
   
   push();
   
   translate(x + hw, y + hw);
   rotate(angle);
   
   beginShape();
   fill(getCol());
   vertex(hs, hs);
   vertex(-hs, hs);
   fill(getCol());
   vertex(-hs, -hs);
   vertex(hs, -hs);
   endShape();
   
   pop();
 }
}

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

class Grid {
 float x, y, w, c;
 int cw;
 
 Grid(float x, float y, float w, float c) {
   this.x = x;
   this.y = y;
   this.w = w;
   this.c = c;
   cw = int(w/c);
 }
}

色々やってるけども、やりたかったのはこれ。

頂点が一つ大きい正方形の辺から飛び出ないように正方形を回転させた。

中身がぐるぐる回るアニメーションも気が向いたら作ろう。

Happy coding!

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