-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
Random.js
38 lines (32 loc) · 1.11 KB
/
Random.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
/**
* @author Richard Davey <rich@phaser.io>
* @copyright 2013-2024 Phaser Studio Inc.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Point = require('../point/Point');
/**
* Returns a uniformly distributed random point from anywhere within the given Circle.
*
* @function Phaser.Geom.Circle.Random
* @since 3.0.0
*
* @generic {Phaser.Geom.Point} O - [out,$return]
*
* @param {Phaser.Geom.Circle} circle - The Circle to get a random point from.
* @param {(Phaser.Geom.Point|object)} [out] - A Point or point-like object to set the random `x` and `y` values in.
*
* @return {(Phaser.Geom.Point|object)} A Point object with the random values set in the `x` and `y` properties.
*/
var Random = function (circle, out)
{
if (out === undefined) { out = new Point(); }
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;