phaser4-rex-plugins
Version:
59 lines (47 loc) • 1.46 kB
JavaScript
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2019 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
import Rectangle from '../rectangle/Rectangle.js';
/**
* Calculates the Axis Aligned Bounding Box (or aabb) from an array of points.
*
* @function Phaser.Geom.Point.GetRectangleFromPoints
* @since 3.0.0
*
* @generic {Phaser.Geom.Rectangle} O - [out,$return]
*
* @param {Phaser.Geom.Point[]} points - [description]
* @param {Phaser.Geom.Rectangle} [out] - [description]
*
* @return {Phaser.Geom.Rectangle} [description]
*/
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;
};
export default GetRectangleFromPoints;