Added Point.GetCentroid.

This commit is contained in:
Richard Davey 2017-01-03 22:31:02 +00:00
parent 52d769fc89
commit 1712560c6e
2 changed files with 39 additions and 0 deletions

View file

@ -0,0 +1,38 @@
var Point = require('./Point');
var GetCentroid = function (points, out)
{
if (out === undefined) { out = new Point(); }
if (!Array.isArray(points))
{
throw new Error('GetCentroid points argument must be an array');
}
var len = points.length;
if (len < 1)
{
throw new Error('GetCentroid points array must not be empty');
}
else if (len === 1)
{
out.x = points[0].x;
out.y = points[0].y;
}
else
{
for (var i = 0; i < len; i++)
{
out.x += points[i].x;
out.y += points[i].y;
}
out.x /= len;
out.y /= len;
}
return out;
};
module.exports = GetCentroid;

View file

@ -11,6 +11,7 @@ Point.Divide = require('./Divide');
Point.Dot = require('./Dot');
Point.Equals = require('./Equals');
Point.Floor = require('./Floor');
Point.GetCentroid = require('./GetCentroid');
Point.GetMagnitude = require('./GetMagnitude');
Point.GetMagnitudeSq = require('./GetMagnitudeSq');
Point.Interpolate = require('./Interpolate');