Skip to content

Commit 1712560

Browse files
committed
Added Point.GetCentroid.
1 parent 52d769f commit 1712560

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

v3/src/geom/point/GetCentroid.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
var Point = require('./Point');
2+
3+
var GetCentroid = function (points, out)
4+
{
5+
if (out === undefined) { out = new Point(); }
6+
7+
if (!Array.isArray(points))
8+
{
9+
throw new Error('GetCentroid points argument must be an array');
10+
}
11+
12+
var len = points.length;
13+
14+
if (len < 1)
15+
{
16+
throw new Error('GetCentroid points array must not be empty');
17+
}
18+
else if (len === 1)
19+
{
20+
out.x = points[0].x;
21+
out.y = points[0].y;
22+
}
23+
else
24+
{
25+
for (var i = 0; i < len; i++)
26+
{
27+
out.x += points[i].x;
28+
out.y += points[i].y;
29+
}
30+
31+
out.x /= len;
32+
out.y /= len;
33+
}
34+
35+
return out;
36+
};
37+
38+
module.exports = GetCentroid;

v3/src/geom/point/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Point.Divide = require('./Divide');
1111
Point.Dot = require('./Dot');
1212
Point.Equals = require('./Equals');
1313
Point.Floor = require('./Floor');
14+
Point.GetCentroid = require('./GetCentroid');
1415
Point.GetMagnitude = require('./GetMagnitude');
1516
Point.GetMagnitudeSq = require('./GetMagnitudeSq');
1617
Point.Interpolate = require('./Interpolate');

0 commit comments

Comments
 (0)