xxxxxxxxxx
Particle[] ps;
void setup(){
size(500, 500);
ps = new Particle[49];
/*for(int i=0; i<ps.length; i++){
ps[i] = new Particle();
}*/
for(int i=0; i<7; i++){
for(int j=0; j<7; j++){
ps[i+7*j] = new Particle(i*70 + 45, j*70 + 45);
}
}
}
void draw(){
fill(255, 10);
rect(0, 0, width, height);
for(int i=0; i<ps.length; i++){
ps[i].exist();
ps[i].interact(ps);
ps[i].constrainedMovement();
}
}
class Particle {
float x, y;
float initX, initY;
float velX, velY;
Particle() {
x = random(width);
y = random(height);
velX = (random(1)<0.5)?random(0, 1):random(-1, 0);
velY = (random(1)<0.5)?random(0, 1):random(-1, 0);
}
Particle(float x_, float y_) {
x = x_;
y = y_;
initX = x;
initY = y;
velX = (random(1)<0.5)?random(0, 1):random(-1, 0);
velY = (random(1)<0.5)?random(0, 1):random(-1, 0);
}
void move() {
x+= velX;
y+=velY;
}
void display() {
fill(200);
noStroke();
ellipse(x, y, 5, 5);
}
void edges() {
if (x>width || x<0) velX*=-1;
if (y>height || y<0) velY*=-1;
}
void interact(Particle[] ps) {
stroke(150);
for (int i=0; i<ps.length; i++) {
if (this.x!=ps[i].x && this.y !=ps[i].y) {
if (dist(this.x, this.y, ps[i].x, ps[i].y)<110) {
line(this.x, this.y, ps[i].x, ps[i].y);
}
}
}
}
void constrainedMovement(){
if(abs(x-initX)>70) velX*=-1;
if(abs(y-initY)>70) velY*=-1;
}
void exist() {
move();
display();
edges();
}
}