Optimize various isCollinear() and isOrthogonal() methods.

This commit is contained in:
Jürg Lehni 2015-10-03 17:42:52 -04:00
parent 8073183010
commit be2f98d91a
2 changed files with 31 additions and 19 deletions

View file

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

View file

@ -690,7 +690,9 @@ var Point = Base.extend(/** @lends Point# */{
* @param {Number} tolerance the maximum distance allowed
* @return {Boolean} {@true if it is within the given distance}
*/
isClose: function(point, tolerance) {
isClose: function(/* point, tolerance */) {
var point = Point.read(arguments),
tolerance = Base.read(arguments);
return this.getDistance(point) < tolerance;
},
@ -701,14 +703,9 @@ var Point = Base.extend(/** @lends Point# */{
* @param {Point} point the vector to check against
* @return {Boolean} {@true it is collinear}
*/
isCollinear: function(point) {
// 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.TRIGONOMETRIC_EPSILON;
isCollinear: function(/* point */) {
var point = Point.read(arguments);
return Point.isCollinear(this.x, this.y, point.x, point.y);
},
// TODO: Remove version with typo after a while (deprecated June 2015)
@ -721,13 +718,9 @@ var Point = Base.extend(/** @lends Point# */{
* @param {Point} point the vector to check against
* @return {Boolean} {@true it is orthogonal}
*/
isOrthogonal: function(point) {
// 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.TRIGONOMETRIC_EPSILON;
isOrthogonal: function(/* point */) {
var point = Point.read(arguments);
return Point.isOrthogonal(this.x, this.y, point.x, point.y);
},
/**
@ -922,6 +915,23 @@ var Point = Base.extend(/** @lends Point# */{
*/
random: function() {
return new Point(Math.random(), Math.random());
},
isCollinear: function(x1, y1, x2, y2) {
// NOTE: We use normalized vectors so that the epsilon comparison is
// reliable. We could instead scale the epsilon based on the vector
// length. But instead of normalizing the vectors before calculating
// the cross product, we can scale the epsilon accordingly.
return Math.abs(x1 * y2 - y1 * x2)
<= Math.sqrt((x1 * x1 + y1 * y1) * (x2 * x2 + y2 * y2))
* /*#=*/Numerical.TRIGONOMETRIC_EPSILON;
},
isOrthogonal: function(x1, y1, x2, y2) {
// See Point.isCollinear()
return Math.abs(x1 * x2 + y1 * y2)
<= Math.sqrt((x1 * x1 + y1 * y1) * (x2 * x2 + y2 * y2))
* /*#=*/Numerical.TRIGONOMETRIC_EPSILON;
}
}
}, Base.each(['round', 'ceil', 'floor', 'abs'], function(name) {