見出し画像

Generative Art #130

Code

ArrayList<Form> forms;
Grid grid;

void setup() {
 size(840, 840);
 pixelDensity(2);
 noLoop();
 grid = new Grid(0, 0, width, 40);
}

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

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 mousePressed() {
 redraw();
 newForm();
}

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

int[] colors = {#02335b,#ff0022,#41ead4,#fdfffc,#c9147a};
int getCol() {
 return colors[(int)random(colors.length)];
}

class Form {
 float x, y, w;
 float hw, ww;
 color col;
 Form(float x, float y, float w) {
   this.x = x;
   this.y = y;
   this.w = w;
   hw = w/2;
   ww = random(hw*0.7, hw);
   col = getCol();
 }
 void show() {
   shadow();
   fill(col);
   noStroke();
   //square(x, y, w);
   push();
   translate(x + w/2, y + w/2);
   beginShape();
   vertex(-hw, -hw);
   vertex(hw, -hw);
   vertex(hw, hw);
   vertex(-hw, hw);
   beginContour();
   vertex(-ww, ww);
   vertex(ww, ww);
   vertex(ww, -ww);
   vertex(-ww, -ww);
   endContour();
   endShape();
   circle(0, 0, ww*0.2);
   pop();
 }

 void shadow() {
   fill(0, 30);
   noStroke();
   for (int i = 0; i < 10; i ++) {
     float shift = map(i, 0, 9, 4, 0);
     float www = ww - shift;
     push();
     translate(x + w/2, y + w/2);
     beginShape();
     vertex(-hw, -hw);
     vertex(hw, -hw);
     vertex(hw, hw);
     vertex(-hw, hw);
     beginContour();
     vertex(-www, www);
     vertex(www, www);
     vertex(www, -www);
     vertex(-www, -www);
     endContour();
     endShape();
     circle(0, 0, ww*0.2 + shift);
     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);
 }
 void show() {
   noStroke();
   fill(0);
   for (float i = x; i <= x + w; i += c) {
     for (float j = y; j <= y + w; j += c) {
       circle(i, j, 1);
     }
   }
 }
}

Happy coding!

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