Skip to content

Instantly share code, notes, and snippets.

@nick3499
Created April 19, 2016 15:25
Show Gist options
  • Select an option

  • Save nick3499/f77ca02a61ade5cdd4eaeb8f4f1f6ba9 to your computer and use it in GitHub Desktop.

Select an option

Save nick3499/f77ca02a61ade5cdd4eaeb8f4f1f6ba9 to your computer and use it in GitHub Desktop.
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];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment