mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
cleanup code a bit
This commit is contained in:
+24
-12
@@ -8,13 +8,15 @@ L.DistanceGrid = function (cellSize) {
|
||||
L.DistanceGrid.prototype = {
|
||||
|
||||
addObject: function (obj, point) {
|
||||
var x = Math.floor(point.x / this._cellSize),
|
||||
y = Math.floor(point.y / this._cellSize),
|
||||
row = this._grid[y] = this._grid[y] || {},
|
||||
cell = row[x] = row[x] || [];
|
||||
var x = this._getCoord(point.x),
|
||||
y = this._getCoord(point.y),
|
||||
grid = this._grid,
|
||||
row = grid[y] = grid[y] || {},
|
||||
cell = row[x] = row[x] || [];
|
||||
|
||||
obj._dGridCell = cell;
|
||||
obj._dGridPoint = point;
|
||||
|
||||
cell.push(obj);
|
||||
},
|
||||
|
||||
@@ -25,18 +27,20 @@ L.DistanceGrid.prototype = {
|
||||
|
||||
removeObject: function (obj) {
|
||||
var oldCell = obj._dGridCell,
|
||||
point = obj._dGridPoint,
|
||||
x, y, i, len;
|
||||
point = obj._dGridPoint,
|
||||
i, len, x, y;
|
||||
|
||||
for (i = 0, len = oldCell.length; i < len; i++) {
|
||||
if (oldCell[i] === obj) {
|
||||
|
||||
oldCell.splice(i, 1);
|
||||
|
||||
if (len === 1) {
|
||||
x = Math.floor(point.x / this._cellSize),
|
||||
y = Math.floor(point.y / this._cellSize),
|
||||
x = this._getCoord(point.x);
|
||||
y = this._getCoord(point.y);
|
||||
delete this._grid[y][x];
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -44,14 +48,16 @@ L.DistanceGrid.prototype = {
|
||||
|
||||
eachObject: function (fn, context) {
|
||||
var i, j, k, len, row, cell, removed,
|
||||
grid = this._grid;
|
||||
grid = this._grid;
|
||||
|
||||
for (i in grid) {
|
||||
if (grid.hasOwnProperty(i)) {
|
||||
row = grid[i];
|
||||
|
||||
for (j in row) {
|
||||
if (row.hasOwnProperty(j)) {
|
||||
cell = row[j];
|
||||
|
||||
for (k = 0, len = cell.length; k < len; k++) {
|
||||
removed = fn.call(context, cell[k]);
|
||||
if (removed) {
|
||||
@@ -66,16 +72,18 @@ L.DistanceGrid.prototype = {
|
||||
},
|
||||
|
||||
getNearObject: function (point) {
|
||||
var x = Math.floor(point.x / this._cellSize),
|
||||
y = Math.floor(point.y / this._cellSize),
|
||||
var x = this._getCoord(point.x),
|
||||
y = this._getCoord(point.y),
|
||||
i, j, k, row, cell, len, obj;
|
||||
|
||||
for (i = y - 1; i <= y + 1; i++) {
|
||||
row = this._grid[i];
|
||||
if (row) {
|
||||
|
||||
for (j = x - 1; j <= x + 1; j++) {
|
||||
cell = row[j];
|
||||
if (cell) {
|
||||
|
||||
for (k = 0, len = cell.length; k < len; k++) {
|
||||
obj = cell[k];
|
||||
if (this._sqDist(obj._dGridPoint, point) < this._sqCellSize) {
|
||||
@@ -90,9 +98,13 @@ L.DistanceGrid.prototype = {
|
||||
return null;
|
||||
},
|
||||
|
||||
_getCoord: function (x) {
|
||||
return Math.floor(x / this._cellSize);
|
||||
},
|
||||
|
||||
_sqDist: function (p, p2) {
|
||||
var dx = p2.x - p.x,
|
||||
dy = p2.y - p.y;
|
||||
dy = p2.y - p.y;
|
||||
return dx * dx + dy * dy;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user