Skip to content

Commit 77e7b2a

Browse files
committed
Ellipse.random will return a uniformly distributed random point from anywhere within the ellipse.
1 parent 3daea17 commit 77e7b2a

4 files changed

Lines changed: 31 additions & 4 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,9 @@ Version 2.4 - "Katar" - in dev
303303
* onDragUpdate is a new signal that is dispatched whenever a Game object enabled for input and drag is moved by a pointer (i.e. during a drag event). See the `Phaser.InputHandler.enableDrag` docs for parameter details and the new Phaser Example.
304304
* Rectangle.resize allows you to resize a Rectangle to the new given dimensions without altering its position.
305305
* Cache.getJSON has a new parameter: `clone`. If set it will return a clone of the object stored in the Cache rather than a reference to it.
306-
* Circle.random will return a random point from anywhere within the circle.
306+
* 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.
308+
* Ellipse.random will return a uniformly distributed random point from anywhere within the ellipse.
308309

309310
### Updates
310311

src/geom/Circle.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,11 @@ Phaser.Circle.prototype = {
7070
},
7171

7272
/**
73-
* Returns a random point from anywhere within this circle.
74-
* The point is uniformly distributed within bounds.
73+
* Returns a uniformly distributed random point from anywhere within this Circle.
7574
*
7675
* @method Phaser.Circle#random
7776
* @param {Phaser.Point|object} [out] - A Phaser.Point, or any object with public x/y properties, that the values will be set in.
78-
* If no object is provided a new Phaser.Point object will be created. In high performance areas avoid this by re-using an object.
77+
* 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.
7978
* @return {Phaser.Point} An object containing the random point in its `x` and `y` properties.
8079
*/
8180
random: function (out) {

src/geom/Ellipse.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ Phaser.Ellipse.prototype = {
137137

138138
/**
139139
* Return true if the given x/y coordinates are within this Ellipse object.
140+
*
140141
* @method Phaser.Ellipse#contains
141142
* @param {number} x - The X value of the coordinate to test.
142143
* @param {number} y - The Y value of the coordinate to test.
@@ -148,6 +149,31 @@ Phaser.Ellipse.prototype = {
148149

149150
},
150151

152+
/**
153+
* Returns a uniformly distributed random point from anywhere within this Ellipse.
154+
*
155+
* @method Phaser.Ellipse#random
156+
* @param {Phaser.Point|object} [out] - A Phaser.Point, or any object with public x/y properties, that the values will be set in.
157+
* 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.
158+
* @return {Phaser.Point} An object containing the random point in its `x` and `y` properties.
159+
*/
160+
random: function (out) {
161+
162+
if (typeof out === 'undefined') { out = new Phaser.Point(); }
163+
164+
var p = Math.random() * Math.PI * 2;
165+
var r = Math.random();
166+
167+
out.x = Math.sqrt(r) * Math.cos(p);
168+
out.y = Math.sqrt(r) * Math.sin(p);
169+
170+
out.x = this.x + (out.x * this.width / 2.0);
171+
out.y = this.y + (out.y * this.height / 2.0);
172+
173+
return out;
174+
175+
},
176+
151177
/**
152178
* Returns a string representation of this object.
153179
* @method Phaser.Ellipse#toString

typescript/phaser.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,7 @@ declare module Phaser {
814814
copyFrom(source: any): Phaser.Ellipse;
815815
copyTo(dest: any): any;
816816
getBounds(): Phaser.Rectangle;
817+
random(out?: Phaser.Point): Phaser.Point;
817818
setTo(x: number, y: number, width: number, height: number): Phaser.Ellipse;
818819
toString(): string;
819820

0 commit comments

Comments
 (0)