Simplify Point#project()

This commit is contained in:
Jürg Lehni 2015-09-06 14:01:04 +02:00
parent d7fb5cd512
commit f91373efd8

View file

@ -721,6 +721,7 @@ var Point = Base.extend(/** @lends Point# */{
isOrthogonal: function(point) { isOrthogonal: function(point) {
// NOTE: Numerical.EPSILON is too small, breaking shape-path-shape // NOTE: Numerical.EPSILON is too small, breaking shape-path-shape
// conversion test. // conversion test.
// TODO: Test if 1e-10 works here too? See #isCollinear()
return Math.abs(this.dot(point)) < /*#=*/Numerical.TOLERANCE; return Math.abs(this.dot(point)) < /*#=*/Numerical.TOLERANCE;
}, },
@ -767,23 +768,19 @@ var Point = Base.extend(/** @lends Point# */{
}, },
/** /**
* Returns the projection of the point on another point. * Returns the projection of the point onto another point.
* Both points are interpreted as vectors. * Both points are interpreted as vectors.
* *
* @param {Point} point * @param {Point} point
* @return {Point} the projection of the point on another point * @return {Point} the projection of the point onto another point
*/ */
project: function(/* point */) { project: function(/* point */) {
var point = Point.read(arguments); var point = Point.read(arguments),
if (point.isZero()) { scale = point.isZero() ? 0 : this.dot(point) / point.dot(point);
return new Point(0, 0); return new Point(
} else { point.x * scale,
var scale = this.dot(point) / point.dot(point); point.y * scale
return new Point( );
point.x * scale,
point.y * scale
);
}
}, },
/** /**