見出し画像

ProcessingでGenerative art #98

Code

int actRandomSeed = (int)random(10000);
int count = 6;

void setup() {
  size(800, 800, P2D);
  pixelDensity(2);
  colorMode(HSB, 360, 100, 100, 100);
  smooth(8);
}

void draw() {
  randomSeed(actRandomSeed);
  background(0);

  for (int i=0; i<100; i++) {
    float size = 200;
    pushMatrix();
    translate(random(width), random(height));
    squareRecursion(random(-size), random(-size), random(size), random(-size), random(size), random(size), -random(size), random(size), count);
    popMatrix();
  }
}

void squareRecursion(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, int n) {
  if (n == count) {
    fill(getCol());
    noStroke();
    beginShape();
    vertex(x1, y1);
    fill(getCol());
    vertex(x2, y2);
    fill(getCol());
    vertex(x3, y3);
    fill(getCol());
    vertex(x4, y4);
    endShape(CLOSE);
  }
  if (n == 1) {
    form(x1, y1, x2, y2, x3, y3, x4, y4);
  }
  float centerX = (((x1+x4)/2) + ((x2+x3)/2))/2;
  float centerY = (((y1+y2)/2) + ((y3+y4)/2))/2;

  n--;
  if (n > 0) {
    squareRecursion(x1, y1, (x1+x2)/2, (y1+y2)/2, centerX, centerY, (x1+x4)/2, (y1+y4)/2, n);
    squareRecursion((x1+x2)/2, (y1+y2)/2, x2, y2, (x2+x3)/2, (y2+y3)/2, centerX, centerY, n);
    squareRecursion(centerX, centerY, (x2+x3)/2, (y2+y3)/2, x3, y3, (x3+x4)/2, (y3+y4)/2, n);
    squareRecursion((x1+x4)/2, (y1+y4)/2, centerX, centerY, (x3+x4)/2, (y3+y4)/2, x4, y4, n);
  }
}

void form(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) {
  noFill();
  stroke(180);
  if (random(1) > 0.5) {
    line(x1, y1, x2, y2);
    line(x3, y3, x4, y4);
  } else {
    line(x1, y1, x4, y4);
    line(x2, y2, x3, y3);
  }
}

int[] colors = {#ffffff, #000000};
int getCol() {
  return colors[(int)random(colors.length)];
}

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

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

Happy coding!!

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