Skip to content

Commit

Permalink
Move and rename function to util.centerFromPerimeter.
Browse files Browse the repository at this point in the history
Add more tests.
  • Loading branch information
manthey committed Jan 16, 2018
1 parent f29c9cf commit 42d552f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 31 deletions.
32 changes: 1 addition & 31 deletions src/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,44 +130,14 @@ var annotation = function (type, args) {
return this;
};

/**
* Return the coordinate associated with the center of the annotation.
*
* @returns {geo.geoPosition|undefined} The map gcs position for the center,
* or `undefined` if no such position exists.
*/
this._centerPosition = function () {
var coor = this._coordinates(), position, p0, p1, w, sumw, i;
if (!coor || !coor.length) {
return undefined;
}
if (coor.length === 1) {
return coor[0];
}
position = {x: 0, y: 0};
sumw = 0;
p0 = coor[coor.length - 1];
for (i = 0; i < coor.length; i += 1) {
p1 = p0;
p0 = coor[i];
w = Math.sqrt(Math.pow(p1.x - p0.x, 2) + Math.pow(p1.y - p0.y, 2));
position.x += (p0.x + p1.x) * w;
position.y += (p0.y + p1.y) * w;
sumw += 2 * w;
}
position.x /= sumw;
position.y /= sumw;
return sumw ? position : p0; // return p0 if all points are the same
};

/**
* Return the coordinate associated with the label.
*
* @returns {geo.geoPosition|undefined} The map gcs position for the label,
* or `undefined` if no such position exists.
*/
this._labelPosition = function () {
return this._centerPosition();
return util.centerFromPerimeter(this._coordinates());
};

/**
Expand Down
33 changes: 33 additions & 0 deletions src/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,39 @@ var util = module.exports = {
return {map: mapParams, layer: layerParams};
},

/**
* Return the coordinate associated with the center of the perimeter formed
* from a list of points.
*
* @param {geo.geoPosition[]} coor An array of coordinates.
* @returns {geo.geoPosition|undefined} The position for the center, or
* `undefined` if no such position exists.
*/
centerFromPerimeter: function (coor) {
var position, p0, p1, w, sumw, i;
if (!coor || !coor.length) {
return;
}
if (coor.length === 1) {
return {x: coor[0].x, y: coor[0].y};
}
position = {x: 0, y: 0};
sumw = 0;
p0 = coor[coor.length - 1];
for (i = 0; i < coor.length; i += 1) {
p1 = p0;
p0 = coor[i];
w = Math.sqrt(Math.pow(p1.x - p0.x, 2) + Math.pow(p1.y - p0.y, 2));
position.x += (p0.x + p1.x) * w;
position.y += (p0.y + p1.y) * w;
sumw += 2 * w;
}
position.x /= sumw;
position.y /= sumw;
// return a copy of p0 if all points are the same
return sumw ? position : {x: p0.x, y: p0.y};
},

/**
* Escape any character in a string that has a code point >= 127.
*
Expand Down
13 changes: 13 additions & 0 deletions tests/cases/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,17 @@ describe('geo.util', function () {
expect(({}) instanceof iframeWindow.Object).toBe(false);
iframe.remove();
});

it('centerFromPerimter', function () {
expect(util.centerFromPerimeter()).toBe(undefined);
expect(util.centerFromPerimeter([])).toBe(undefined);
expect(util.centerFromPerimeter([{x: 1, y: 1}])).toEqual({x: 1, y: 1});
expect(util.centerFromPerimeter([{x: 1, y: 1}, {x: 1, y: 1}])).toEqual({x: 1, y: 1});
expect(util.centerFromPerimeter([
{x: 1, y: 1}, {x: 3, y: 1}, {x: 3, y: 3}, {x: 1, y: 3}
])).toEqual({x: 2, y: 2});
expect(util.centerFromPerimeter([
{x: 1, y: 1}, {x: 3, y: 1}, {x: 5, y: 1}, {x: 5, y: 3}, {x: 1, y: 3}
])).toEqual({x: 3, y: 2});
});
});

0 comments on commit 42d552f

Please sign in to comment.