phaser/src/geom/circle/Random.js

39 lines
1.1 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2019-01-15 16:20:22 +00:00
* @copyright 2019 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
*/
var Point = require('../point/Point');
2016-12-29 00:17:20 +00:00
/**
* Returns a uniformly distributed random point from anywhere within the given Circle.
*
* @function Phaser.Geom.Circle.Random
* @since 3.0.0
*
2018-03-27 13:27:08 +00:00
* @generic {Phaser.Geom.Point} O - [out,$return]
*
2018-01-26 04:18:22 +00:00
* @param {Phaser.Geom.Circle} circle - The Circle to get a random point from.
2018-03-27 13:27:08 +00:00
* @param {(Phaser.Geom.Point|object)} [out] - A Point or point-like object to set the random `x` and `y` values in.
2017-10-13 13:11:54 +00:00
*
2018-03-20 15:01:08 +00:00
* @return {(Phaser.Geom.Point|object)} A Point object with the random values set in the `x` and `y` properties.
*/
2016-12-29 00:17:20 +00:00
var Random = function (circle, out)
{
if (out === undefined) { out = new Point(); }
2016-12-29 00:17:20 +00:00
var t = 2 * Math.PI * Math.random();
var u = Math.random() + Math.random();
var r = (u > 1) ? 2 - u : u;
var x = r * Math.cos(t);
var y = r * Math.sin(t);
out.x = circle.x + (x * circle.radius);
out.y = circle.y + (y * circle.radius);
return out;
};
module.exports = Random;