2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2020-01-15 12:07:09 +00:00
|
|
|
* @copyright 2020 Photon Storm Ltd.
|
2019-05-10 15:15:04 +00:00
|
|
|
* @license {@link https://opensource.org/licenses/MIT|MIT License}
|
2018-02-12 16:01:20 +00:00
|
|
|
*/
|
|
|
|
|
2017-09-29 10:40:49 +00:00
|
|
|
var Rectangle = require('./Rectangle');
|
2019-11-18 17:01:55 +00:00
|
|
|
var MATH_CONST = require('../../math/const');
|
2017-09-29 10:40:49 +00:00
|
|
|
|
|
|
|
// points is an array of Point-like objects,
|
|
|
|
// either 2 dimensional arrays, or objects with public x/y properties:
|
|
|
|
// var points = [
|
|
|
|
// [100, 200],
|
|
|
|
// [200, 400],
|
|
|
|
// { x: 30, y: 60 }
|
|
|
|
// ]
|
|
|
|
|
2017-10-13 13:11:54 +00:00
|
|
|
/**
|
2018-10-01 10:35:01 +00:00
|
|
|
* Constructs new Rectangle or repositions and resizes an existing Rectangle so that all of the given points are on or within its bounds.
|
2017-10-13 13:11:54 +00:00
|
|
|
*
|
|
|
|
* @function Phaser.Geom.Rectangle.FromPoints
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-27 13:27:08 +00:00
|
|
|
* @generic {Phaser.Geom.Rectangle} O - [out,$return]
|
|
|
|
*
|
2018-10-01 10:35:01 +00:00
|
|
|
* @param {array} points - An array of points (either arrays with two elements corresponding to the X and Y coordinate or an object with public `x` and `y` properties) which should be surrounded by the Rectangle.
|
|
|
|
* @param {Phaser.Geom.Rectangle} [out] - Optional Rectangle to adjust.
|
2017-10-13 13:11:54 +00:00
|
|
|
*
|
2018-10-01 10:35:01 +00:00
|
|
|
* @return {Phaser.Geom.Rectangle} The adjusted `out` Rectangle, or a new Rectangle if none was provided.
|
2017-10-13 13:11:54 +00:00
|
|
|
*/
|
2017-09-29 10:40:49 +00:00
|
|
|
var FromPoints = function (points, out)
|
|
|
|
{
|
|
|
|
if (out === undefined) { out = new Rectangle(); }
|
|
|
|
|
|
|
|
if (points.length === 0)
|
|
|
|
{
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2018-01-18 14:59:32 +00:00
|
|
|
var minX = Number.MAX_VALUE;
|
|
|
|
var minY = Number.MAX_VALUE;
|
2017-09-29 10:40:49 +00:00
|
|
|
|
2019-11-18 17:01:55 +00:00
|
|
|
var maxX = MATH_CONST.MIN_SAFE_INTEGER;
|
|
|
|
var maxY = MATH_CONST.MIN_SAFE_INTEGER;
|
2017-09-29 10:40:49 +00:00
|
|
|
|
|
|
|
var p;
|
|
|
|
var px;
|
|
|
|
var py;
|
|
|
|
|
|
|
|
for (var i = 0; i < points.length; i++)
|
|
|
|
{
|
|
|
|
p = points[i];
|
|
|
|
|
|
|
|
if (Array.isArray(p))
|
|
|
|
{
|
|
|
|
px = p[0];
|
|
|
|
py = p[1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
px = p.x;
|
|
|
|
py = p.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
minX = Math.min(minX, px);
|
|
|
|
minY = Math.min(minY, py);
|
|
|
|
|
|
|
|
maxX = Math.max(maxX, px);
|
|
|
|
maxY = Math.max(maxY, py);
|
|
|
|
}
|
|
|
|
|
|
|
|
out.x = minX;
|
|
|
|
out.y = minY;
|
|
|
|
out.width = maxX - minX;
|
|
|
|
out.height = maxY - minY;
|
|
|
|
|
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = FromPoints;
|