phaser/src/geom/point/GetRectangleFromPoints.js

65 lines
1.6 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2020-01-15 12:07:09 +00:00
* @copyright 2020 Photon Storm Ltd.
2019-05-10 15:15:04 +00:00
* @license {@link https://opensource.org/licenses/MIT|MIT License}
2018-02-12 16:01:20 +00:00
*/
var Rectangle = require('../rectangle/Rectangle');
/**
2018-01-26 13:14:41 +00:00
* Calculates the Axis Aligned Bounding Box (or aabb) from an array of points.
2017-10-13 13:11:54 +00:00
*
* @function Phaser.Geom.Point.GetRectangleFromPoints
* @since 3.0.0
*
2018-03-27 13:27:08 +00:00
* @generic {Phaser.Geom.Rectangle} O - [out,$return]
*
2020-02-04 16:19:42 +00:00
* @param {Phaser.Types.Math.Vector2Like[]} points - An array of Vector2Like objects to get the AABB from.
* @param {Phaser.Geom.Rectangle} [out] - A Rectangle object to store the results in. If not given, a new Rectangle instance is created.
2017-10-13 13:11:54 +00:00
*
2020-02-04 16:19:42 +00:00
* @return {Phaser.Geom.Rectangle} A Rectangle object holding the AABB values for the given points.
2017-10-13 13:11:54 +00:00
*/
var GetRectangleFromPoints = function (points, out)
{
if (out === undefined) { out = new Rectangle(); }
var xMax = Number.NEGATIVE_INFINITY;
var xMin = Number.POSITIVE_INFINITY;
var yMax = Number.NEGATIVE_INFINITY;
var yMin = Number.POSITIVE_INFINITY;
for (var i = 0; i < points.length; i++)
{
var point = points[i];
if (point.x > xMax)
{
xMax = point.x;
}
if (point.x < xMin)
{
xMin = point.x;
}
if (point.y > yMax)
{
yMax = point.y;
}
if (point.y < yMin)
{
yMin = point.y;
}
}
out.x = xMin;
out.y = yMin;
out.width = xMax - xMin;
out.height = yMax - yMin;
return out;
};
module.exports = GetRectangleFromPoints;