Another optimization would be to compare square of distance instead of distance, saving "Math.sqrt" call each iteration.
Another optimization would be to compare square of distance instead of distance, saving "Math.sqrt" call each iteration.