arcade-physics
Version:
Use Arcade Physics without Phaser.
47 lines • 1.65 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 Point_1 = __importDefault(require("../point/Point"));
/**
* Returns a random Point from within the area of the given Triangle.
*
* @function Phaser.Geom.Triangle.Random
* @since 3.0.0
*
* @generic {Phaser.Geom.Point} O - [out,$return]
*
* @param {Phaser.Geom.Triangle} triangle - The Triangle to get a random point from.
* @param {Phaser.Geom.Point} [out] - The Point object to store the position in. If not given, a new Point instance is created.
*
* @return {Phaser.Geom.Point} A Point object holding the coordinates of a random position within the Triangle.
*/
const Random = (triangle, out) => {
if (out === undefined) {
out = new Point_1.default();
}
// Basis vectors
const ux = triangle.x2 - triangle.x1;
const uy = triangle.y2 - triangle.y1;
const vx = triangle.x3 - triangle.x1;
const vy = triangle.y3 - triangle.y1;
// Random point within the unit square
let r = Math.random();
let s = Math.random();
// Point outside the triangle? Remap it.
if (r + s >= 1) {
r = 1 - r;
s = 1 - s;
}
out.x = triangle.x1 + (ux * r + vx * s);
out.y = triangle.y1 + (uy * r + vy * s);
return out;
};
exports.default = Random;
//# sourceMappingURL=Random.js.map