I've been scratching my head trying to understand why this code isn't working.
var refP = [];
var calculateDistance = function (p1, p2) {
return dist(p1.x, p1.y, p2.x, p2.y);
}
while (refP.length < 24) {
var point = {
x: -1,
y: -1,
closestRefPoint: 9999999
};
point.x = (random(0, 400));
point.y = (random(0, 400));
for (var i = 0; i < refP.length; i++) {
if (calculateDistance(point, refP[i]) < point.closestRefPoint) {
point.closestRefPoint = calculateDistance(point, refP[i]);
}
}
if (point.closestRefPoint > 2) {
refP[refP.length] = point;
}
}
After removing the last if-statement and unconditionally pushing 'point' onto 'refP', the loop doesn't seem to work as expected.
Any help or suggestions on how to clean up or simplify this code would be greatly appreciated. Thank you!