Make Line.isCollinear() / Point#isCollinear() more reliable.

This commit is contained in:
Jürg Lehni 2015-09-23 13:26:29 -04:00
parent cc7e60e51a
commit 515d4ff93d
2 changed files with 16 additions and 4 deletions

View file

@ -113,8 +113,9 @@ var Line = Base.extend(/** @lends Line# */{
},
isCollinear: function(line) {
return this._vx * line._vy - this._vy * line._vx
< /*#=*/Numerical.GEOMETRIC_EPSILON;
// TODO: Optimize:
// return Point.isCollinear(this._vx, this._vy, line._vx, line._vy);
return this.getVector().isCollinear(line.getVector());
},
statics: /** @lends Line */{

View file

@ -702,7 +702,13 @@ var Point = Base.extend(/** @lends Point# */{
* @return {Boolean} {@true it is collinear}
*/
isCollinear: function(point) {
return Math.abs(this.cross(point)) < /*#=*/Numerical.GEOMETRIC_EPSILON;
// NOTE: We use normalized vectors so that the epsilon comparison is
// reliable. We could instead scale the epsilon based on the vector
// length.
// TODO: Optimize by creating a static Point.isCollinear() to be used
// in Line.isCollinear() as well.
return Math.abs(this.normalize().cross(point.normalize()))
< /*#=*/Numerical.GEOMETRIC_EPSILON;
},
// TODO: Remove version with typo after a while (deprecated June 2015)
@ -716,7 +722,12 @@ var Point = Base.extend(/** @lends Point# */{
* @return {Boolean} {@true it is orthogonal}
*/
isOrthogonal: function(point) {
return Math.abs(this.dot(point)) < /*#=*/Numerical.GEOMETRIC_EPSILON;
// NOTE: We use normalized vectors so that the epsilon comparison is
// reliable. We could instead scale the epsilon based on the vector
// length.
// TODO: Optimize
return Math.abs(this.normalize().dot(point.normalize()))
< /*#=*/Numerical.GEOMETRIC_EPSILON;
},
/**