Skip to content

Commit 42d552f

Browse files
committed
Move and rename function to util.centerFromPerimeter.
Add more tests.
1 parent f29c9cf commit 42d552f

File tree

3 files changed

+47
-31
lines changed

3 files changed

+47
-31
lines changed

src/annotation.js

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -130,44 +130,14 @@ var annotation = function (type, args) {
130130
return this;
131131
};
132132

133-
/**
134-
* Return the coordinate associated with the center of the annotation.
135-
*
136-
* @returns {geo.geoPosition|undefined} The map gcs position for the center,
137-
* or `undefined` if no such position exists.
138-
*/
139-
this._centerPosition = function () {
140-
var coor = this._coordinates(), position, p0, p1, w, sumw, i;
141-
if (!coor || !coor.length) {
142-
return undefined;
143-
}
144-
if (coor.length === 1) {
145-
return coor[0];
146-
}
147-
position = {x: 0, y: 0};
148-
sumw = 0;
149-
p0 = coor[coor.length - 1];
150-
for (i = 0; i < coor.length; i += 1) {
151-
p1 = p0;
152-
p0 = coor[i];
153-
w = Math.sqrt(Math.pow(p1.x - p0.x, 2) + Math.pow(p1.y - p0.y, 2));
154-
position.x += (p0.x + p1.x) * w;
155-
position.y += (p0.y + p1.y) * w;
156-
sumw += 2 * w;
157-
}
158-
position.x /= sumw;
159-
position.y /= sumw;
160-
return sumw ? position : p0; // return p0 if all points are the same
161-
};
162-
163133
/**
164134
* Return the coordinate associated with the label.
165135
*
166136
* @returns {geo.geoPosition|undefined} The map gcs position for the label,
167137
* or `undefined` if no such position exists.
168138
*/
169139
this._labelPosition = function () {
170-
return this._centerPosition();
140+
return util.centerFromPerimeter(this._coordinates());
171141
};
172142

173143
/**

src/util/index.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,39 @@ var util = module.exports = {
745745
return {map: mapParams, layer: layerParams};
746746
},
747747

748+
/**
749+
* Return the coordinate associated with the center of the perimeter formed
750+
* from a list of points.
751+
*
752+
* @param {geo.geoPosition[]} coor An array of coordinates.
753+
* @returns {geo.geoPosition|undefined} The position for the center, or
754+
* `undefined` if no such position exists.
755+
*/
756+
centerFromPerimeter: function (coor) {
757+
var position, p0, p1, w, sumw, i;
758+
if (!coor || !coor.length) {
759+
return;
760+
}
761+
if (coor.length === 1) {
762+
return {x: coor[0].x, y: coor[0].y};
763+
}
764+
position = {x: 0, y: 0};
765+
sumw = 0;
766+
p0 = coor[coor.length - 1];
767+
for (i = 0; i < coor.length; i += 1) {
768+
p1 = p0;
769+
p0 = coor[i];
770+
w = Math.sqrt(Math.pow(p1.x - p0.x, 2) + Math.pow(p1.y - p0.y, 2));
771+
position.x += (p0.x + p1.x) * w;
772+
position.y += (p0.y + p1.y) * w;
773+
sumw += 2 * w;
774+
}
775+
position.x /= sumw;
776+
position.y /= sumw;
777+
// return a copy of p0 if all points are the same
778+
return sumw ? position : {x: p0.x, y: p0.y};
779+
},
780+
748781
/**
749782
* Escape any character in a string that has a code point >= 127.
750783
*

tests/cases/util.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,17 @@ describe('geo.util', function () {
3232
expect(({}) instanceof iframeWindow.Object).toBe(false);
3333
iframe.remove();
3434
});
35+
36+
it('centerFromPerimter', function () {
37+
expect(util.centerFromPerimeter()).toBe(undefined);
38+
expect(util.centerFromPerimeter([])).toBe(undefined);
39+
expect(util.centerFromPerimeter([{x: 1, y: 1}])).toEqual({x: 1, y: 1});
40+
expect(util.centerFromPerimeter([{x: 1, y: 1}, {x: 1, y: 1}])).toEqual({x: 1, y: 1});
41+
expect(util.centerFromPerimeter([
42+
{x: 1, y: 1}, {x: 3, y: 1}, {x: 3, y: 3}, {x: 1, y: 3}
43+
])).toEqual({x: 2, y: 2});
44+
expect(util.centerFromPerimeter([
45+
{x: 1, y: 1}, {x: 3, y: 1}, {x: 5, y: 1}, {x: 5, y: 3}, {x: 1, y: 3}
46+
])).toEqual({x: 3, y: 2});
47+
});
3548
});

0 commit comments

Comments
 (0)