ProcessingでGenerative art #65
見出し画像

ProcessingでGenerative art #65

みんな大好きNode garden

頭の中にはこんな風にいじりたいなーとイメージは沢山あるのでもう少し遊びたい。

Code

int actRandomSeed = 0;
int count = 110;
Node [] nodes = new Node[count];
int drawMode = 1;
float maxLength = 120;

void setup() {
  size(800, 800);
  pixelDensity(2);
  smooth();
  noStroke();
  
  for (int i = 0; i < count; i++) {
    nodes[i] = new Node();
  }
}

void draw() {
  randomSeed(actRandomSeed);

  if (drawMode == 1)background(255, 245, 245);
  if (drawMode == 2)background(245, 255, 245);
  if (drawMode == 3)background(245, 245, 255);

  for (int i = 0; i < count -1; i++) {
    Node thisNode = nodes[i];
    for (int j = 0; j < count; j++) {
      Node otherNode = nodes[j];
      drawLine(thisNode, otherNode);
    }
  }
  
  for (int i = 0; i < count; i++) {
    nodes[i].run();
  }
}

void drawLine(Node n1, Node n2) {
  float distance = dist(n1.position.x, n1.position.y, n2.position.x, n2.position.y);
  if (distance <= maxLength) {
    strokeWeight(0.8);
    stroke(0);
    line(n1.position.x, n1.position.y, n2.position.x, n2.position.y);
  }
  
}

void mousePressed() {
  for (int i = 0; i < count; i++) {
    nodes[i] = new Node();
  }
  actRandomSeed = (int)random(10000);
}

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

//------------------------------------------------------------------

class Node {
  PVector position;
  PVector velocity;
  float ellipseSize = 10;
  Node() {
    position = new PVector(random(width), random(height));
    velocity = new PVector(random(-2, 2), random(-2, 2));
    ellipseSize = random(10, 55);
  }

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

  void display() {
    if (drawMode == 1)fill(255, random(255), random(255));
    if (drawMode == 2)fill(random(255), 255, random(255));
    if (drawMode == 3)fill(random(255), random(255), 255);
    stroke(0);
    strokeWeight(2);
    ellipse(position.x, position.y, ellipseSize, ellipseSize);
  }

  void update() {
    position.add(velocity);
    checkHitWall();
  }

  void checkHitWall() {
    if (position.x < 0 + ellipseSize/2) {
      velocity.x *= -1;
      position.x = 0 + ellipseSize/2;
    } else if (position.x > width - ellipseSize/2) {
      velocity.x *= -1;
      position.x = width - ellipseSize/2;
    } else if (position.y < 0 + ellipseSize/2) {
      velocity.y *= -1;
      position.y = 0 + ellipseSize/2;
    } else if (position.y > height - ellipseSize/2) {
      velocity.y *= -1;
      position.y = height - ellipseSize/2;
    }
  }
}

Happy Coding!

この記事が気に入ったら、サポートをしてみませんか?
気軽にクリエイターの支援と、記事のオススメができます!
おかず

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

もっと押して!