with
Boid.prototype.moveWith = function(boids, distance) {
if(boids.length < 1) return
// calculate the average velocity of the other boids
var avgX = 0;
var avgY = 0;
for(var i = 0; i < boids.length; i++) {
var boid = boids[i];
if(boid.x == this.x && boid.y == this.y) continue;
if(this.distance(boid) > distance) continue;
avgX += boid.xVelocity;
avgY += boid.yVelocity;
boid = null;
}
avgX /= boids.length;
avgY /= boids.length;
distance = Math.sqrt((avgX * avgX) + (avgY * avgY)) * 1.0
if(distance == 0) return;
this.xVelocity= Math.min(this.xVelocity + (avgX / distance) * 0.05, maxVelocity)
this.yVelocity = Math.min(this.yVelocity + (avgY / distance) * 0.05, maxVelocity)
}
var random = function(maxNum) {
return Math.ceil(Math.random() * maxNum);
}