mirror of
https://github.com/photonstorm/phaser
synced 2024-11-24 13:43:26 +00:00
Merge pull request #1199 from codevinsky/feature/rectangle-aabb
Rectangle.aabb
This commit is contained in:
commit
49ddf46ef3
1 changed files with 45 additions and 4 deletions
|
@ -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.
|
// Because PIXI uses its own Rectangle, we'll replace it with ours to avoid duplicating code or confusion.
|
||||||
PIXI.Rectangle = Phaser.Rectangle;
|
PIXI.Rectangle = Phaser.Rectangle;
|
||||||
PIXI.EmptyRectangle = new Phaser.Rectangle(0, 0, 0, 0);
|
PIXI.EmptyRectangle = new Phaser.Rectangle(0, 0, 0, 0);
|
||||||
|
|
Loading…
Reference in a new issue