Skip to content

Commit 17e8cb7

Browse files
Support DistanceGrid with cell size 0
For extremely small cell sizes, fall back to returning the original coordinate in _getCoord(), instead of Infinity. Together with the previous change, this groups objects with identical coordinates into the same cell and therefore fixes #836 for maxClusterRadius values like 0 or Number.MIN_VALUE. (However, Number.EPSILON is actually large enough that dividing normal values for x by it does not yet yield Infinity, so in that case the bug persists.) We use isFinite() instead of Number.isFinite() because the latter is not as widely supported (in particular, it doesn’t work in `jake test`). Since the argument is the result of Math.floor, it should always be a number, so the difference should not matter.
1 parent cc61460 commit 17e8cb7

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

spec/suites/DistanceGridSpec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,14 @@
2828
expect(grid.getNearObject({ x: 50, y: 50 })).to.equal(obj);
2929
expect(grid.getNearObject({ x: 100, y: 0 })).to.equal(obj);
3030
});
31+
32+
it('getNearObject with cellSize 0', function () {
33+
var grid = new L.DistanceGrid(0),
34+
obj = {};
35+
36+
grid.addObject(obj, { x: 0, y: 0 });
37+
38+
expect(grid.getNearObject({ x: 50, y: 50 })).to.equal(null);
39+
expect(grid.getNearObject({ x: 0, y: 0 })).to.equal(obj);
40+
});
3141
});

src/DistanceGrid.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ L.DistanceGrid.prototype = {
106106
},
107107

108108
_getCoord: function (x) {
109-
return Math.floor(x / this._cellSize);
109+
var coord = Math.floor(x / this._cellSize);
110+
return isFinite(coord) ? coord : x;
110111
},
111112

112113
_sqDist: function (p, p2) {

0 commit comments

Comments
 (0)