nick34992
4/19/2016 - 3:25 PM

Array. Using processing.js library.

Array. Using processing.js library.

float [] x;
float [] y;
float [] xv;
float [] yv;
int population = 100;

void setup() {
  size(1024, 576);
  frameRate(60);
  x = new float[population];
  y = new float[population];
  xv = new float[population];
  yv = new float[population];
  for(int i=0; i<population; i++) {
    // random location
    x[i] = random(20, 1000);
    y[i] = random(20, 1000);
    // random velocity
    xv[i] = random(-4, 4);
    yv[i] = random(-2, 2);
  }
}

void draw() {
  background(0);
  fill(255, 0, 0);
  stroke(0, 0, 255, 100);
  strokeWeight(20);
  for(int i=0; i<population; i++) {
    ellipse(x[i], y[i], 40, 40);
    x[i] = x[i] + xv[i];
    y[i] = y[i] + yv[i];
    // invert velocity
    if((x[i] > width - 1) || (x[i] < 1)) {
      xv[i] = -xv[i]; 
    }
    if((y[i] > height - 1) || (y[i] < 1)) {
      yv[i] = -yv[i]; 
    }
  }
}