Merge pull request #1199 from codevinsky/feature/rectangle-aabb

Rectangle.aabb
This commit is contained in:
Richard Davey 2014-09-19 13:50:14 +01:00
commit 49ddf46ef3

View file

@ -838,6 +838,47 @@ Phaser.Rectangle.union = function (a, b, output) {
};
/**
* Calculates the Axis Aligned Bounding Box (or aabb) from an array of points.
*
* @method Phaser.Rectangle#aabb
* @param {Phaser.Point[]} points - The array of one or more points.
* @param {Phaser.Rectangle} [out] - Optional Rectangle to store the value in, if not supplied a new Rectangle object will be created.
* @return {Phaser.Rectangle} The new Rectangle object.
* @static
*/
Phaser.Rectangle.aabb = function(points, out) {
if (typeof out === "undefined") {
out = new Phaser.Rectangle();
}
var xMax = Number.MIN_VALUE,
xMin = Number.MAX_VALUE,
yMax = Number.MIN_VALUE,
yMin = Number.MAX_VALUE;
points.forEach(function(point) {
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.setTo(xMin, yMin, xMax - xMin, yMax - yMin);
return out;
};
// Because PIXI uses its own Rectangle, we'll replace it with ours to avoid duplicating code or confusion.
PIXI.Rectangle = Phaser.Rectangle;
PIXI.EmptyRectangle = new Phaser.Rectangle(0, 0, 0, 0);