Further simplify new algorithm.

This commit is contained in:
Jürg Lehni 2013-05-07 00:04:16 -07:00
parent 9ddb1132b0
commit bfc5b5eba4
2 changed files with 11 additions and 17 deletions

View file

@ -1222,9 +1222,6 @@ new function() { // Scope for methods that require numerical integration
return { return {
getNearestLocation: function(point) { getNearestLocation: function(point) {
// NOTE: If we allow #matrix on Path, we need to inverse-transform
// point here first.
// point = this._matrix.inverseTransform(point);
var w = toBezierForm(this.getPoints(), point); var w = toBezierForm(this.getPoints(), point);
// Also look at beginning and end of curve (t = 0 / 1) // Also look at beginning and end of curve (t = 0 / 1)
var roots = findRoots(w, 0).concat([0, 1]); var roots = findRoots(w, 0).concat([0, 1]);
@ -1248,36 +1245,31 @@ new function() { // Scope for methods that require numerical integration
_getNearestLocation: function(point) { _getNearestLocation: function(point) {
var values = this.getValues(), var values = this.getValues(),
precision = 1 / 100, step = 1 / 100,
tolerance = Numerical.TOLERANCE, tolerance = Numerical.TOLERANCE,
minDist = Infinity, minDist = Infinity,
minT = 0, minT = 0,
max = 1 + tolerance; // Accomodate imprecision max = 1 + tolerance; // Accomodate imprecision
for (var t = 0; t <= max; t += precision) {
var pt = Curve.evaluate(values, t, true, 0),
dist = point.getDistance(pt, true);
if (dist < minDist) {
minDist = dist;
minT = t;
}
}
function refine(t) { function refine(t) {
if (t >= 0 && t <= 1) { if (t >= 0 && t <= 1) {
var dist = point.getDistance( var dist = point.getDistance(
Curve.evaluate(values, t, true, 0), true); Curve.evaluate(values, t, true, 0), true);
if (dist < minDist) { if (dist < minDist) {
minT = t;
minDist = dist; minDist = dist;
minT = t;
return true; return true;
} }
} }
} }
while (precision > tolerance) { for (var t = 0; t <= max; t += step)
if (!refine(minT - precision) && !refine(minT + precision)) refine(t);
precision /= 2;
step /= 2;
while (step > tolerance) {
if (!refine(minT - step) && !refine(minT + step))
step /= 2;
} }
var pt = Curve.evaluate(values, minT, true, 0); var pt = Curve.evaluate(values, minT, true, 0);
return new CurveLocation(this, minT, pt, null, point.getDistance(pt)); return new CurveLocation(this, minT, pt, null, point.getDistance(pt));

View file

@ -1539,6 +1539,7 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{
* the specified point * the specified point
*/ */
getNearestLocation: function(point) { getNearestLocation: function(point) {
point = this._matrix.inverseTransform(Point.read(arguments));
var curves = this.getCurves(), var curves = this.getCurves(),
minDist = Infinity, minDist = Infinity,
minLoc = null; minLoc = null;
@ -1553,6 +1554,7 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{
}, },
_getNearestLocation: function(point) { _getNearestLocation: function(point) {
point = this._matrix.inverseTransform(Point.read(arguments));
var curves = this.getCurves(), var curves = this.getCurves(),
minDist = Infinity, minDist = Infinity,
minLoc = null; minLoc = null;