-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
RandomOutside.js
65 lines (56 loc) · 2.38 KB
/
RandomOutside.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013-2023 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Between = require('../../math/Between');
var ContainsRect = require('./ContainsRect');
var Point = require('../point/Point');
/**
* Calculates a random point that lies within the `outer` Rectangle, but outside of the `inner` Rectangle.
* The inner Rectangle must be fully contained within the outer rectangle.
*
* @function Phaser.Geom.Rectangle.RandomOutside
* @since 3.10.0
*
* @generic {Phaser.Geom.Point} O - [out,$return]
*
* @param {Phaser.Geom.Rectangle} outer - The outer Rectangle to get the random point within.
* @param {Phaser.Geom.Rectangle} inner - The inner Rectangle to exclude from the returned point.
* @param {Phaser.Geom.Point} [out] - A Point, or Point-like object to store the result in. If not specified, a new Point will be created.
*
* @return {Phaser.Geom.Point} A Point object containing the random values in its `x` and `y` properties.
*/
var RandomOutside = function (outer, inner, out)
{
if (out === undefined) { out = new Point(); }
if (ContainsRect(outer, inner))
{
// Pick a random quadrant
//
// The quadrants don't extend the full widths / heights of the outer rect to give
// us a better uniformed distribution, otherwise you get clumping in the corners where
// the 4 quads would overlap
switch (Between(0, 3))
{
case 0: // Top
out.x = outer.x + (Math.random() * (inner.right - outer.x));
out.y = outer.y + (Math.random() * (inner.top - outer.y));
break;
case 1: // Bottom
out.x = inner.x + (Math.random() * (outer.right - inner.x));
out.y = inner.bottom + (Math.random() * (outer.bottom - inner.bottom));
break;
case 2: // Left
out.x = outer.x + (Math.random() * (inner.x - outer.x));
out.y = inner.y + (Math.random() * (outer.bottom - inner.y));
break;
case 3: // Right
out.x = inner.right + (Math.random() * (outer.right - inner.right));
out.y = outer.y + (Math.random() * (inner.bottom - outer.y));
break;
}
}
return out;
};
module.exports = RandomOutside;