Skip to content

Commit 3daea17

Browse files
committed
Line.random will return a random point from anywhere on the Line segment.
1 parent 70cf7a3 commit 3daea17

4 files changed

Lines changed: 25 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ Version 2.4 - "Katar" - in dev
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.
306306
* Circle.random will return a random point from anywhere within the circle.
307+
* Line.random will return a random point from anywhere on the Line segment.
307308

308309
### Updates
309310

src/geom/Circle.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ Phaser.Circle.prototype = {
7575
*
7676
* @method Phaser.Circle#random
7777
* @param {Phaser.Point|object} [out] - A Phaser.Point, or any object with public x/y properties, that the values will be set in.
78-
* @return {number} The circumference of the circle.
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.
79+
* @return {Phaser.Point} An object containing the random point in its `x` and `y` properties.
7980
*/
8081
random: function (out) {
8182

src/geom/Line.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,27 @@ Phaser.Line.prototype = {
166166

167167
},
168168

169+
/**
170+
* Picks a random point from anywhere on the Line segment and returns it.
171+
*
172+
* @method Phaser.Line#random
173+
* @param {Phaser.Point|object} [out] - A Phaser.Point, or any object with public x/y properties, that the values will be set in.
174+
* If no object is provided a new Phaser.Point object will be created. In high performance areas avoid this by re-using an object.
175+
* @return {Phaser.Point} An object containing the random point in its `x` and `y` properties.
176+
*/
177+
random: function (out) {
178+
179+
if (typeof out === 'undefined') { out = new Phaser.Point(); }
180+
181+
var t = Math.random();
182+
183+
out.x = this.start.x + t * (this.end.x - this.start.x);
184+
out.y = this.start.y + t * (this.end.y - this.start.y);
185+
186+
return out;
187+
188+
},
189+
169190
/**
170191
* Using Bresenham's line algorithm this will return an array of all coordinates on this line.
171192
* The start and end points are rounded before this runs as the algorithm works on integers.

typescript/phaser.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1985,6 +1985,7 @@ declare module Phaser {
19851985
intersects(line: Phaser.Line, asSegment?: boolean, result?: Phaser.Point): Phaser.Point;
19861986
pointOnLine(x: number, y: number): boolean;
19871987
pointOnSegment(x: number, y: number): boolean;
1988+
random(out?: Phaser.Point): Phaser.Point;
19881989
reflect(line: Phaser.Line): number;
19891990
setTo(x1?: number, y1?: number, x2?: number, y2?: number): Phaser.Line;
19901991

0 commit comments

Comments
 (0)