Make getNearObject always return the closest point to make the clusters do stupid things less often

This commit is contained in:
danzel
2012-08-27 13:02:45 +12:00
parent 8ac19a8f65
commit c1aff5cba2
+8 -5
View File
@@ -74,7 +74,9 @@ L.DistanceGrid.prototype = {
getNearObject: function (point) {
var x = this._getCoord(point.x),
y = this._getCoord(point.y),
i, j, k, row, cell, len, obj;
i, j, k, row, cell, len, obj, dist,
closestDistSq = this._sqCellSize,
closest = null;
for (i = y - 1; i <= y + 1; i++) {
row = this._grid[i];
@@ -86,16 +88,17 @@ L.DistanceGrid.prototype = {
for (k = 0, len = cell.length; k < len; k++) {
obj = cell[k];
if (this._sqDist(obj._dGridPoint, point) < this._sqCellSize) {
return obj;
dist = this._sqDist(obj._dGridPoint, point);
if (dist < closestDistSq) {
closestDistSq = dist;
closest = obj;
}
}
}
}
}
}
return null;
return closest;
},
_getCoord: function (x) {