diff --git a/README.md b/README.md index c3f0ee44f..adc3f550b 100644 --- a/README.md +++ b/README.md @@ -295,6 +295,7 @@ If you code with [TypeScript](http://www.typescriptlang.org/) there are comprehe ### Updates +* Added an `epsilon` argument for fuzzy comparisons in Phaser.Line#pointOnLine and Phaser.Line#pointOnSegment (#312). * Removed obsolete PIXI TypeScript definitions. * Removed [filters/pixi](https://github.com/photonstorm/phaser-ce/tree/v2.8.3/filters/pixi). They require PIXI.AbstractFilter, which was removed in 2.7.0. * Updated NPM dependencies (except [typescript](https://www.npmjs.com/package/typescript); photonstorm/phaser#2198) and added [package-lock.json](https://docs.npmjs.com/files/package-lock.json). diff --git a/src/geom/Line.js b/src/geom/Line.js index d53bfd502..ce4e55149 100644 --- a/src/geom/Line.js +++ b/src/geom/Line.js @@ -222,35 +222,37 @@ Phaser.Line.prototype = { }, /** - * Tests if the given coordinates fall on this line. See pointOnSegment to test against just the line segment. + * Tests if the given coordinates fall on this line. See {@link #pointOnSegment} to test against just the line segment. * * @method Phaser.Line#pointOnLine * @param {number} x - The line to check against this one. * @param {number} y - The line to check against this one. + * @param {number} [epsilon=0] - Range for a fuzzy comparison, e.g., 0.0001. * @return {boolean} True if the point is on the line, false if not. */ - pointOnLine: function (x, y) { + pointOnLine: function (x, y, epsilon) { - return ((x - this.start.x) * (this.end.y - this.start.y) === (this.end.x - this.start.x) * (y - this.start.y)); + return Phaser.Math.fuzzyEqual((x - this.start.x) * (this.end.y - this.start.y), (this.end.x - this.start.x) * (y - this.start.y), epsilon || 0); }, /** - * Tests if the given coordinates fall on this line and within the segment. See pointOnLine to test against just the line. + * Tests if the given coordinates fall on this line and within the segment. See {@link #pointOnLine} to test against just the line. * * @method Phaser.Line#pointOnSegment * @param {number} x - The line to check against this one. * @param {number} y - The line to check against this one. + * @param {number} [epsilon=0] - Range for a fuzzy comparison, e.g., 0.0001. * @return {boolean} True if the point is on the line and segment, false if not. */ - pointOnSegment: function (x, y) { + pointOnSegment: function (x, y, epsilon) { var xMin = Math.min(this.start.x, this.end.x); var xMax = Math.max(this.start.x, this.end.x); var yMin = Math.min(this.start.y, this.end.y); var yMax = Math.max(this.start.y, this.end.y); - return (this.pointOnLine(x, y) && (x >= xMin && x <= xMax) && (y >= yMin && y <= yMax)); + return (this.pointOnLine(x, y, epsilon) && (x >= xMin && x <= xMax) && (y >= yMin && y <= yMax)); }, diff --git a/typescript/phaser.d.ts b/typescript/phaser.d.ts index 3a204675c..8da28ecbd 100644 --- a/typescript/phaser.d.ts +++ b/typescript/phaser.d.ts @@ -2467,8 +2467,8 @@ declare module Phaser { fromSprite(startSprite: Phaser.Sprite, endSprite: Phaser.Sprite, useCenter?: boolean): Phaser.Line; intersects(line: Phaser.Line, asSegment?: boolean, result?: Phaser.Point): Phaser.Point; midPoint(out?: Phaser.Point): Phaser.Point; - pointOnLine(x: number, y: number): boolean; - pointOnSegment(x: number, y: number): boolean; + pointOnLine(x: number, y: number, epsilon?: number): boolean; + pointOnSegment(x: number, y: number, epsilon?: number): boolean; random(out?: Phaser.Point): Phaser.Point; reflect(line: Phaser.Line): number; rotate(angle: number, asDegrees?: boolean): Phaser.Line;