Skip to content

Commit ab9da10

Browse files
committed
added: Rectangle.aabb
* 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
1 parent 5a58d6b commit ab9da10

1 file changed

Lines changed: 45 additions & 4 deletions

File tree

src/geom/Rectangle.js

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ Phaser.Rectangle.prototype = {
9696

9797
/**
9898
* Centers this Rectangle so that the center coordinates match the given x and y values.
99-
*
99+
*
100100
* @method Phaser.Rectangle#centerOn
101101
* @param {number} x - The x coordinate to place the center of the Rectangle at.
102102
* @param {number} y - The y coordinate to place the center of the Rectangle at.
@@ -269,7 +269,7 @@ Phaser.Rectangle.prototype = {
269269

270270
/**
271271
* Determines whether the coordinates given intersects (overlaps) with this Rectangle.
272-
*
272+
*
273273
* @method Phaser.Rectangle#intersectsRaw
274274
* @param {number} left - The x coordinate of the left of the area.
275275
* @param {number} right - The right coordinate of the area.
@@ -482,7 +482,7 @@ Object.defineProperty(Phaser.Rectangle.prototype, "centerY", {
482482

483483
/**
484484
* A random value between the left and right values (inclusive) of the Rectangle.
485-
*
485+
*
486486
* @name Phaser.Rectangle#randomX
487487
* @property {number} randomX - A random value between the left and right values (inclusive) of the Rectangle.
488488
*/
@@ -498,7 +498,7 @@ Object.defineProperty(Phaser.Rectangle.prototype, "randomX", {
498498

499499
/**
500500
* A random value between the top and bottom values (inclusive) of the Rectangle.
501-
*
501+
*
502502
* @name Phaser.Rectangle#randomY
503503
* @property {number} randomY - A random value between the top and bottom values (inclusive) of the Rectangle.
504504
*/
@@ -838,6 +838,47 @@ Phaser.Rectangle.union = function (a, b, output) {
838838

839839
};
840840

841+
/**
842+
* Calculates the Axis Aligned Bounding Box (or aabb) from an array of points.
843+
*
844+
* @method Phaser.Rectangle#aabb
845+
* @param {Phaser.Point[]} points - The array of one or more points.
846+
* @param {Phaser.Rectangle} [out] - Optional Rectangle to store the value in, if not supplied a new Rectangle object will be created.
847+
* @return {Phaser.Rectangle} The new Rectangle object.
848+
* @static
849+
*/
850+
Phaser.Rectangle.aabb = function(points, out) {
851+
852+
if (typeof out === "undefined") {
853+
out = new Phaser.Rectangle();
854+
}
855+
856+
var xMax = Number.MIN_VALUE,
857+
xMin = Number.MAX_VALUE,
858+
yMax = Number.MIN_VALUE,
859+
yMin = Number.MAX_VALUE;
860+
861+
points.forEach(function(point) {
862+
if (point.x > xMax) {
863+
xMax = point.x;
864+
}
865+
if (point.x < xMin) {
866+
xMin = point.x;
867+
}
868+
869+
if (point.y > yMax) {
870+
yMax = point.y;
871+
}
872+
if (point.y < yMin) {
873+
yMin = point.y;
874+
}
875+
});
876+
877+
out.setTo(xMin, yMin, xMax - xMin, yMax - yMin);
878+
879+
return out;
880+
};
881+
841882
// Because PIXI uses its own Rectangle, we'll replace it with ours to avoid duplicating code or confusion.
842883
PIXI.Rectangle = Phaser.Rectangle;
843884
PIXI.EmptyRectangle = new Phaser.Rectangle(0, 0, 0, 0);

0 commit comments

Comments
 (0)