[Pkg-javascript-commits] [leaflet-markercluster] 154/479: cleanup code a bit

Jonas Smedegaard dr at jones.dk
Thu Oct 16 16:00:24 UTC 2014


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository leaflet-markercluster.

commit 17522d8fc338a0be96224d7afa66d6f4e6bc377f
Author: mourner <agafonkin at gmail.com>
Date:   Thu Aug 9 23:12:30 2012 +0300

    cleanup code a bit
---
 src/DistanceGrid.js | 36 ++++++++++++++++++++++++------------
 1 file changed, 24 insertions(+), 12 deletions(-)

diff --git a/src/DistanceGrid.js b/src/DistanceGrid.js
index f1c699e..70df5fb 100644
--- a/src/DistanceGrid.js
+++ b/src/DistanceGrid.js
@@ -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;
 	}
 };

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/leaflet-markercluster.git



More information about the Pkg-javascript-commits mailing list