見出し画像

ProcessingでGenerative art #76

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 = 5;
float maxRadius = 200;

int[] hueValues = new int[maxCount];
int[] saturationValues = new int[maxCount];
int[] brightnessValues = new int[maxCount];

void setup() {
  size(800, 800);
  pixelDensity(2);
  smooth();
  colorMode(HSB, 360, 100, 100, 100);
  rectMode(CENTER);

  x[0] = width/2;
  y[0] = height/2;
  closestIndex[0] = 0;

  for (int i=0; i<maxCount; i++) {
    hueValues[i] = (int) random(360);
    saturationValues[i] = (int) random(0, 100);
    brightnessValues[i] = (int) random(0, 100);
  }
}

void draw() {

  background(360);

  for (int i = 0; i < 20; i ++) {
    update();
  }
  drawShape();

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

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

  if (mousePressed == true) {
    newX = random(mouseX-20, mouseX+20);
    newY = random(mouseY-20, mouseY+20);
    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++) {
    noStroke();

    float fillHue = hueValues[i];
    circle(x[i], y[i], r[i]*2, i, fillHue);
    int n = closestIndex[i];

    strokeWeight(1);
    stroke(hueValues[i], saturationValues[i] - 50, 100);
    line(x[i], y[i], x[n], y[n]);

    stroke(hueValues[i], saturationValues[i] - 50, 100, 50);
    line(x[i], y[i], x[1], y[1]);
  }
}

void circle(float posX, float posY, float diameter, int index, float hue) {
  noStroke();
  for (int d = (int)diameter; d > 0; d -= 10) {
    fill(hue, saturationValues[index + d], brightnessValues[index + d]);
    ellipse(posX, posY, d, d);
  }
}

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

Happy coding!!

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