Skip to content

Commit 20e21ff

Browse files
committed
Rectangle.RandomOutside is a new function that takes two Rectangles, outer and inner, and returns a random point that falls within the outer rectangle but is always outside of the inner rectangle.
1 parent 594fc3e commit 20e21ff

3 files changed

Lines changed: 71 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### New Features
66

77
* RenderTexture.resize will allow you to resize the underlying Render Texture to the new dimensions given. Doing this also clears the Render Texture at the same time (thanks @saqsun)
8+
* Rectangle.RandomOutside is a new function that takes two Rectangles, `outer` and `inner`, and returns a random point that falls within the outer rectangle but is always outside of the inner rectangle.
89

910
### Updates
1011

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2018 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
var Between = require('../../math/Between');
8+
var ContainsRect = require('./ContainsRect');
9+
var Point = require('../point/Point');
10+
11+
/**
12+
* Calculates a random point that lies within the `outer` Rectangle, but outside of the `inner` Rectangle.
13+
* The inner Rectangle must be fully contained within the outer rectangle.
14+
*
15+
* @function Phaser.Geom.Rectangle.RandomOutside
16+
* @since 3.10.0
17+
*
18+
* @generic {Phaser.Geom.Point} O - [out,$return]
19+
*
20+
* @param {Phaser.Geom.Rectangle} outer - The outer Rectangle to get the random point within.
21+
* @param {Phaser.Geom.Rectangle} inner - The inner Rectangle to exclude from the returned point.
22+
* @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.
23+
*
24+
* @return {Phaser.Geom.Point} A Point object containing the random values in its `x` and `y` properties.
25+
*/
26+
var RandomOutside = function (outer, inner, out)
27+
{
28+
if (out === undefined) { out = new Point(); }
29+
30+
if (ContainsRect(outer, inner))
31+
{
32+
// Pick a random quadrant
33+
//
34+
// The quadrants don't extend the full widths / heights of the outer rect to give
35+
// us a better uniformed distribution, otherwise you get clumping in the corners where
36+
// the 4 quads would overlap
37+
38+
switch (Between(0, 3))
39+
{
40+
// Top
41+
case 0:
42+
out.x = outer.x + (Math.random() * (inner.right - outer.x));
43+
out.y = outer.y + (Math.random() * (inner.top - outer.y));
44+
break;
45+
46+
// Bottom
47+
case 1:
48+
out.x = inner.x + (Math.random() * (outer.right - inner.x));
49+
out.y = inner.bottom + (Math.random() * (outer.bottom - inner.bottom));
50+
break;
51+
52+
// Left
53+
case 2:
54+
out.x = outer.x + (Math.random() * (inner.x - outer.x));
55+
out.y = inner.y + (Math.random() * (outer.bottom - inner.y));
56+
break;
57+
58+
// Right
59+
case 3:
60+
out.x = inner.right + (Math.random() * (outer.right - inner.right));
61+
out.y = outer.y + (Math.random() * (inner.bottom - outer.y));
62+
break;
63+
}
64+
}
65+
66+
return out;
67+
};
68+
69+
module.exports = RandomOutside;

src/geom/rectangle/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Rectangle.Overlaps = require('./Overlaps');
3838
Rectangle.Perimeter = require('./Perimeter');
3939
Rectangle.PerimeterPoint = require('./PerimeterPoint');
4040
Rectangle.Random = require('./Random');
41+
Rectangle.RandomOutside = require('./RandomOutside');
4142
Rectangle.Scale = require('./Scale');
4243
Rectangle.Union = require('./Union');
4344

0 commit comments

Comments
 (0)