Move and rename function to util.centerFromPerimeter.
Add more tests.
MODIFY
src/annotation.js
src/annotation.js
@@ -130,36 +130,6 @@ 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.
*
@@ -167,7 +137,7 @@ var annotation = function (type, args) {
* or `undefined` if no such position exists.
*/
this._labelPosition = function () {
- return this._centerPosition();
+ return util.centerFromPerimeter(this._coordinates());
};
/**

MODIFY
src/util/index.js
src/util/index.js
@@ -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.
*

MODIFY
tests/cases/util.js
tests/cases/util.js
@@ -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});
+ });
});

Move and rename function to util.centerFromPerimeter.

Add more tests.