arcade-physics
Version:
Use Arcade Physics without Phaser.
69 lines • 2.31 kB
JavaScript
;
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const Rectangle_1 = __importDefault(require("./Rectangle"));
const const_1 = __importDefault(require("../../math/const"));
// 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 }
// ]
/**
* Constructs new Rectangle or repositions and resizes an existing Rectangle so that all of the given points are on or within its bounds.
*
* @function Phaser.Geom.Rectangle.FromPoints
* @since 3.0.0
*
* @generic {Phaser.Geom.Rectangle} O - [out,$return]
*
* @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.
*
* @return {Phaser.Geom.Rectangle} The adjusted `out` Rectangle, or a new Rectangle if none was provided.
*/
const FromPoints = (points, out) => {
if (out === undefined) {
out = new Rectangle_1.default();
}
if (points.length === 0) {
return out;
}
let minX = Number.MAX_VALUE;
let minY = Number.MAX_VALUE;
let maxX = const_1.default.MIN_SAFE_INTEGER;
let maxY = const_1.default.MIN_SAFE_INTEGER;
let p;
let px;
let py;
for (let 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;
};
exports.default = FromPoints;
//# sourceMappingURL=FromPoints.js.map