Skip to content

Commit 9e78cd1

Browse files
committed
Rectangle.random will return a uniformly distributed random point from anywhere within the rectangle.
1 parent 77e7b2a commit 9e78cd1

3 files changed

Lines changed: 21 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ Version 2.4 - "Katar" - in dev
306306
* Circle.random will return a uniformly distributed random point from anywhere within the circle.
307307
* Line.random will return a random point from anywhere on the Line segment.
308308
* Ellipse.random will return a uniformly distributed random point from anywhere within the ellipse.
309+
* Rectangle.random will return a uniformly distributed random point from anywhere within the rectangle.
309310

310311
### Updates
311312

src/geom/Rectangle.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,25 @@ Phaser.Rectangle.prototype = {
364364

365365
},
366366

367+
/**
368+
* Returns a uniformly distributed random point from anywhere within this Rectangle.
369+
*
370+
* @method Phaser.Rectangle#random
371+
* @param {Phaser.Point|object} [out] - A Phaser.Point, or any object with public x/y properties, that the values will be set in.
372+
* If no object is provided a new Phaser.Point object will be created. In high performance areas avoid this by re-using an existing object.
373+
* @return {Phaser.Point} An object containing the random point in its `x` and `y` properties.
374+
*/
375+
random: function (out) {
376+
377+
if (typeof out === 'undefined') { out = new Phaser.Point(); }
378+
379+
out.x = this.randomX;
380+
out.y = this.randomY;
381+
382+
return out;
383+
384+
},
385+
367386
/**
368387
* Returns a string representation of this object.
369388
* @method Phaser.Rectangle#toString

typescript/phaser.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3680,6 +3680,7 @@ declare module Phaser {
36803680
intersectsRaw(left: number, right: number, top: number, bottom: number, tolerance: number): boolean;
36813681
offset(dx: number, dy: number): Phaser.Rectangle;
36823682
offsetPoint(point: Phaser.Point): Phaser.Rectangle;
3683+
random(out?: Phaser.Point): Phaser.Point;
36833684
resize(width: number, height: number): Phaser.Rectangle;
36843685
setTo(x: number, y: number, width: number, height: number): Phaser.Rectangle;
36853686
scale(x: number, y?: number): Phaser.Rectangle;

0 commit comments

Comments
 (0)