From 3daea179fbc540416bdc3c79e42e1f6d28cdf5bd Mon Sep 17 00:00:00 2001 From: photonstorm Date: Sat, 13 Jun 2015 03:04:02 +0100 Subject: [PATCH] Line.random will return a random point from anywhere on the Line segment. --- README.md | 1 + src/geom/Circle.js | 3 ++- src/geom/Line.js | 21 +++++++++++++++++++++ typescript/phaser.d.ts | 1 + 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d34436685..0f38b9916 100644 --- a/README.md +++ b/README.md @@ -304,6 +304,7 @@ Version 2.4 - "Katar" - in dev * Rectangle.resize allows you to resize a Rectangle to the new given dimensions without altering its position. * 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. * Circle.random will return a random point from anywhere within the circle. +* Line.random will return a random point from anywhere on the Line segment. ### Updates diff --git a/src/geom/Circle.js b/src/geom/Circle.js index a1455ed4b..cb6d3e3db 100644 --- a/src/geom/Circle.js +++ b/src/geom/Circle.js @@ -75,7 +75,8 @@ Phaser.Circle.prototype = { * * @method Phaser.Circle#random * @param {Phaser.Point|object} [out] - A Phaser.Point, or any object with public x/y properties, that the values will be set in. - * @return {number} The circumference of the circle. + * If no object is provided a new Phaser.Point object will be created. In high performance areas avoid this by re-using an object. + * @return {Phaser.Point} An object containing the random point in its `x` and `y` properties. */ random: function (out) { diff --git a/src/geom/Line.js b/src/geom/Line.js index a70e57bb1..e97c701f0 100644 --- a/src/geom/Line.js +++ b/src/geom/Line.js @@ -166,6 +166,27 @@ Phaser.Line.prototype = { }, + /** + * Picks a random point from anywhere on the Line segment and returns it. + * + * @method Phaser.Line#random + * @param {Phaser.Point|object} [out] - A Phaser.Point, or any object with public x/y properties, that the values will be set in. + * If no object is provided a new Phaser.Point object will be created. In high performance areas avoid this by re-using an object. + * @return {Phaser.Point} An object containing the random point in its `x` and `y` properties. + */ + random: function (out) { + + if (typeof out === 'undefined') { out = new Phaser.Point(); } + + var t = Math.random(); + + out.x = this.start.x + t * (this.end.x - this.start.x); + out.y = this.start.y + t * (this.end.y - this.start.y); + + return out; + + }, + /** * Using Bresenham's line algorithm this will return an array of all coordinates on this line. * The start and end points are rounded before this runs as the algorithm works on integers. diff --git a/typescript/phaser.d.ts b/typescript/phaser.d.ts index 5b3c390c2..14bdf593c 100644 --- a/typescript/phaser.d.ts +++ b/typescript/phaser.d.ts @@ -1985,6 +1985,7 @@ declare module Phaser { intersects(line: Phaser.Line, asSegment?: boolean, result?: Phaser.Point): Phaser.Point; pointOnLine(x: number, y: number): boolean; pointOnSegment(x: number, y: number): boolean; + random(out?: Phaser.Point): Phaser.Point; reflect(line: Phaser.Line): number; setTo(x1?: number, y1?: number, x2?: number, y2?: number): Phaser.Line;