Line.random will return a random point from anywhere on the Line segment.

This commit is contained in:
photonstorm 2015-06-13 03:04:02 +01:00
parent 70cf7a32bc
commit 3daea179fb
4 changed files with 25 additions and 1 deletions

View file

@ -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

View file

@ -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) {

View file

@ -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.

View file

@ -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;