mirror of
https://github.com/photonstorm/phaser
synced 2024-11-23 21:24:09 +00:00
Added Point.GetCentroid.
This commit is contained in:
parent
52d769fc89
commit
1712560c6e
2 changed files with 39 additions and 0 deletions
38
v3/src/geom/point/GetCentroid.js
Normal file
38
v3/src/geom/point/GetCentroid.js
Normal 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;
|
|
@ -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');
|
||||
|
|
Loading…
Reference in a new issue