見出し画像

ProcessingでGenerative art #89

Code

ArrayList<Form> forms;
Grid grid;
int actRandomSeed = 0;

void setup() {
  size(800, 800, P2D);
  pixelDensity(2);
  colorMode(HSB, 360, 100, 100, 100);
  grid = new Grid(20, 20, 760, 760, 40);
  newForms();
}

void draw() {
  randomSeed(actRandomSeed);
  background(0);
  for (int i = 0; i < forms.size(); i ++) {
    Form f = forms.get(i);
    f.display();
  }
  grid.display();
}

void newForms() {
  forms = new ArrayList<Form>();

  boolean useGrid[][] = new boolean[grid.cw][grid.cw];
  int emptys = grid.cw*grid.cw;
  while (emptys > 0) {
    float t = grid.t;
    int w = int(random(1, grid.cw));
    int h = int(random(1, grid.ch));
    int x = int(random(grid.cw-w+1));
    int y = int(random(grid.ch-h+1));
    boolean colision = true;
    for (int j = 0; j < h; j++) {
      for (int i = 0; i < w; i++) {
        if (useGrid[x+i][y+j]) {
          colision = false;
          break;
        }
      }
    }
    if (colision) {
      for (int j = 0; j < h; j ++) {
        for (int i = 0; i < w; i ++) {
          useGrid[x+i][y+j] = true;
        }
      }
      forms.add(new Form(grid.x + x*t, grid.y+y*t, w*t, h*t));
      emptys -= w*h;
    }
  }
}

void mousePressed() {
  newForms();
  actRandomSeed = int(random(100000));
}

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

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

class Form {
  color col;
  float x, y, w, h;
  float toggle;
  Form(float x, float y, float w, float h) {
    this.x = x;
    this.y = y;
    this.w = w;
    this.h= h;

    col = color(random(360), 100, 100);
  }

  void display() {
    drawRect();
    noFill();
    strokeWeight(0.5);
    stroke(360);
    if (w < h) {
      drawCircle(x + w/2, y + h/2, w, w);
    } else {
      drawCircle(x + w/2, y + h/2, h, h);
    }
  }

  color randomCol() {
    float r = random(1);
    if (r > 6.0/6.0) return color(0, 100, 100);
    else if (r > 5.0/6.0) return color(62, 100, 100);
    else if (r > 3.0/6.0) return color(155, 100, 100);
    else if (r > 2.0/6.0) return color(216, 100, 100);
    else if (r > 1.0/6.0) return color(318, 100, 100);
    else return color(0);
  }

  void drawCircle(float x_, float y_, float w_, float h_) {
    noFill();
    strokeWeight(0.4);
    for (float i=w_; i>w_/2; i-=1) {
      stroke(360, map(i, w_, w_/2, 100, 0));
      ellipse(x_, y_, i, map(i, w_, w_/2, h_, h_/2));
    }
  }

  void drawRect() {
    fill(randomCol());
    for (float j = y; j < y + h; j += h/2) {
      for (float i = x; i <= x + w - (w/10); i += w/2) {
        toggle = random(1);
        noStroke();
        gradationRect(i, j, w/2, h/2);
      }
    }
    if (random(1) > 0.5) {
      gradationRect(x, y, w, h);
    }
    noFill();
    stroke(360);
    rect(x, y, w, h);
  }

  void gradationRect(float x_, float y_, float w_, float h_) {
    push();
    if (random(1) > 3.0/4.0) {
      beginShape();
      vertex(x_, y_);
      vertex(x_+w_, y_);
      fill(randomCol());
      vertex(x_+w_, y_+h_);
      vertex(x_, y_+h_);
      endShape();
    } else if (random(1) > 2.0/4.0) {
      beginShape();
      vertex(x_, y_+h_);
      vertex(x_, y_);
      fill(randomCol());
      vertex(x_+w_, y_);
      vertex(x_+w_, y_+h_);
      endShape();
    } else if (random(1) > 1.0/4.0) {
      beginShape();
      vertex(x_+w_, y_);
      vertex(x_+w_, y_+h_);
      fill(randomCol());
      vertex(x_, y_+h_);
      vertex(x_, y_);
      endShape();
    } else {
      beginShape();
      vertex(x_+w_, y_+h_);
      vertex(x_, y_+h_);
      fill(randomCol());
      vertex(x_, y_);
      vertex(x_+w_, y_);
      endShape();
    }
    pop();
  }
}

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

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

  void display() {
    noStroke();
    fill(360);
    for (float j = y; j <= y+h; j+=t) {
      for (float i = x; i <= x+w; i+=t) {
        circle(i, j, 4);
      }
    }
  }
}

Happy coding!!

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