見出し画像

ProcessingでGenerative art #55

Code

int maxCount = 5000;
int currentCount = 1;
float [] x = new float [maxCount];
float [] y = new float[maxCount];
float [] r = new float[maxCount];
int [] closestIndex = new int[maxCount];

float minRadius = 3;
float maxRadius = 70;

void setup() {
  size(750, 750);
  pixelDensity(2);
  smooth();
  colorMode(HSB, 360, 100, 100, 100);

  x[0] = 200;
  y[0] = 100;
  closestIndex[0] = 0;
}

void draw() {
  background(360);
  update();
  drawShape();

  if (currentCount >= maxCount)noLoop();
}

void update() {
  float newX = random(0+maxRadius, width-maxRadius);
  float newY = random(0+maxRadius, height-maxRadius);
  float newR = minRadius;

  if (mousePressed == true) {
    newX = mouseX;
    newY = mouseY;
    newR = 1;
  }

  boolean intersection = false;

  for (int i=0; i<currentCount; i++) {
    float d = dist(newX, newY, x[i], y[i]);
    if (d < (newR + r[i])) {
      intersection = true;
      break;
    }
  }

  if (intersection == false) {
    float newRadius = width;
    for (int i=0; i < currentCount; i++) {
      float d = dist(newX, newY, x[i], y[i]);
      if (newRadius > d-r[i]) {
        newRadius = d-r[i];
        closestIndex[currentCount] = i;
      }
    }

    if (newRadius > maxRadius)newRadius = maxRadius;

    x[currentCount] = newX;
    y[currentCount] = newY;
    r[currentCount] = newRadius;
    currentCount++;
  }
}

void drawShape() {
  for (int i=0; i < currentCount; i++) {
    circle(x[i], y[i], r[i]*2);
    stroke(350, 89, 95);
    strokeWeight(0.75);
    int n = closestIndex[i];
    line(x[i], y[i], x[n], y[n]);
  }
}

void circle(float x, float y, float d) {
  strokeWeight(0.5);
  stroke(0);
  if (d > 70) fill(213, 80, 90, 10);
  else if (d > 10) fill(50, 80, 90, 10);
  else {
    noStroke();
    fill(350, 89, 95);
  }
  for (float i=d; i>d/2; i -= 3) {
    ellipse(x, y, i, i);
    noStroke();
  }
}

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

参考:『Generative Design-Processingで切り拓く、デザインの新たな地平』

Happy coding!

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