見出し画像

ProcessingでGenerative art #99

Code

int count = 4;

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

void draw() {
  background(#ffffff);
  squareRecursion(0, 0, width, count);
}

void squareRecursion(float x, float y, float s, int n) {
  noStroke();
  fill(getCol());
  square(x, y, s);
  form(x, y, s);

  n--;
  if (n >= 0) {
    float probability = map(n, 0, count-1, 0.8, 0);
    float hs = s/2;
    if (random(1) > probability) {
      squareRecursion(x, y, hs, n);
    }
    if (random(1) > probability) {
      squareRecursion(x+hs, y, hs, n);
    }
    if (random(1) > probability) {
      squareRecursion(x+hs, y+hs, hs, n);
    }
    if (random(1) > probability) {
      squareRecursion(x, y+hs, hs, n);
    }
  }
}

void form(float x, float y, float s) {
  float hs = s/2;
  push();
  noStroke();
  fill(getCol());
  translate(x+hs, y+hs);
  if (random(1) > 4.0/5.0) {
    for (float i = s; i > 0; i-= s/5) {
      rectMode(CENTER);
      fill(getCol());
      square(0, 0, i);
    }
  } else if (random(1) > 3.0/5.0) {
    rotate(HALF_PI*(int)random(4));
    for (float i = s; i > 0; i-= s/5) {
      fill(getCol());
      square(-hs, -hs, i);
    }
  } else if (random(1) > 2.0/5.0) {
    rotate(HALF_PI*(int)random(2));
    for (float i = s; i > 0; i-= s/5) {
      fill(getCol());
      rect(-hs, -hs, s, i);
    }
  } else if (random(1) > 1.0/5.0) {
    rotate(HALF_PI*(int)random(4));
    for (float i = hs; i > 0; i -= s/5) {
      float h = map(i, hs, 0, s*0.8, 0);
      fill(getCol());
      beginShape();
      vertex(-i, hs);
      vertex(-i/2, hs -h);
      vertex(i/2, hs-h);
      vertex(i, hs);
      endShape();
    }
  } else {
    fill(getCol());
    for (float j = -hs; j < hs; j+= (s/5)*2) {
      for (float i = -hs; i < hs; i+= (s/5)*2) {
        square(i, j, s/5);
      }
    }
    for (float j = -hs + (s/5); j < hs; j+= (s/5)*2) {
      for (float i = -hs + (s/5); i < hs; i+= (s/5)*2) {
        square(i, j, s/5);
      }
    }
  }
  pop();
}

int[] colors = {#0A2463, #000000, #FB3640, #247BA0, #E2E2E2};
int getCol() {
  return colors[(int)random(colors.length)];
}

void mousePressed() {
  redraw();
}

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

Happy coding!

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