-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
GetRectangleFromPoints.js
64 lines (53 loc) · 1.58 KB
/
GetRectangleFromPoints.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Rectangle = require('../rectangle/Rectangle');
/**
* 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.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.
*
* @return {Phaser.Geom.Rectangle} A Rectangle object holding the AABB values for the given points.
*/
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;