見出し画像

ProcessingでGenerative art #92

Code

int pCount = 100;
Particle[] p= new Particle[pCount];
int drawMode = 1;

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

void draw() {
  for (int i=0; i<p.length; i++) {
    p[i].run();
  }
}

void newParticle() {
  background(360);
  for (int i=0; i<p.length; i++) {
    p[i] = new Particle();
  }
}

void mousePressed() {
  newParticle();
}

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

  if (key == '1') drawMode = 1;
  if (key == '2') drawMode = 2;
  if (key == '3') drawMode = 3;
  newParticle();
}

//----------------------------------------------------------------------
class Particle {
  PVector pos, vel;
  float direction; 
  float size, maxSize, minSize;
  float t, tStep;
  color col, c1, c2;

  Particle() {
    pos = new PVector(random(width), random(height));
    getDirection();
    t = random(10);
    minSize = random(5);
    maxSize = random(minSize, 50);
    tStep = 0.02;
    c1 = getRandomColor();
    c2 = getRandomColor();
    getDrawMode();
    
    noStroke();
  }

  void getDirection() {
    float step = random(0.2, 0.8);
    if (random(1) > 0.75) vel = new PVector(step, step);
    else if (random(1) > 0.50) vel = new PVector(-step, step);
    else if (random(1) > 0.25) vel = new PVector(step, -step);
    else vel = new PVector(-step, -step);
  }

  void run() {
    display();
    update();
    checkEdge();
  }

  void display() {
    fill(col, 70);
    if (drawMode == 3) {
      square(pos.x, pos.y, size);
    } else {
      circle(pos.x, pos.y, size);
    }
  }

  void update() {
    pos.add(vel);
    t += tStep;
    size = map(sin(t), -1, 1, minSize, maxSize);
    col = lerpColor(c1, c2, map(sin(t), -1, 1, 0, 1));
  }

  void checkEdge() {
    if (pos.x > width + size/2) {
      pos = new PVector(random(width), random(height));
    }
    if (pos.x < 0 - size/2) {
      pos = new PVector(random(width), random(height));
    }
    if (pos.y > height + size/2) {
      pos = new PVector(random(width), random(height));
    }
    if (pos.x < 0 - size/2) {
      pos = new PVector(random(width), random(height));
    }
  }

  int[] colors = {#FF1A1A, #FFA108, #1A2904, #FF00AA, #FFF300, #F0F1FA};
  int getRandomColor() {
    return colors[(int)random(colors.length)];
  }

  void getDrawMode() {
    if (drawMode == 1) {
      minSize = random(5);
      maxSize = random(minSize, 50);
    }
    if (drawMode == 2 ) {
      minSize = random(1);
      maxSize = random(minSize, 20);
    }
  }
}

Happy coding!!


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