From bff9e1aa980758436c7643aaf6f0058610e3bec6 Mon Sep 17 00:00:00 2001 From: David Manthey Date: Tue, 16 Jan 2018 16:33:44 -0500 Subject: [PATCH] Move and rename function to util.centerFromPerimeter. Add more tests. --- src/annotation.js | 32 +------------------------------- src/util/index.js | 33 +++++++++++++++++++++++++++++++++ tests/cases/util.js | 13 +++++++++++++ 3 files changed, 47 insertions(+), 31 deletions(-) diff --git a/src/annotation.js b/src/annotation.js index bc2a923d24..2ead4bbcb1 100644 --- a/src/annotation.js +++ b/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()); }; /** diff --git a/src/util/index.js b/src/util/index.js index 6138ffe350..7ddeec75fa 100644 --- a/src/util/index.js +++ b/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. * diff --git a/tests/cases/util.js b/tests/cases/util.js index e860526eff..20cda76e5b 100644 --- a/tests/cases/util.js +++ b/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}); + }); });