From bfc5b5eba4829c4b6ef93d7bdd1743a9238a8e09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Tue, 7 May 2013 00:04:16 -0700 Subject: [PATCH] Further simplify new algorithm. --- src/path/Curve.js | 26 +++++++++----------------- src/path/Path.js | 2 ++ 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/path/Curve.js b/src/path/Curve.js index 58bf08af..6bd8ca4a 100644 --- a/src/path/Curve.js +++ b/src/path/Curve.js @@ -1222,9 +1222,6 @@ new function() { // Scope for methods that require numerical integration return { 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); // Also look at beginning and end of curve (t = 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) { var values = this.getValues(), - precision = 1 / 100, + step = 1 / 100, tolerance = Numerical.TOLERANCE, minDist = Infinity, minT = 0, 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) { if (t >= 0 && t <= 1) { var dist = point.getDistance( Curve.evaluate(values, t, true, 0), true); if (dist < minDist) { - minT = t; minDist = dist; + minT = t; return true; } } } - while (precision > tolerance) { - if (!refine(minT - precision) && !refine(minT + precision)) - precision /= 2; + for (var t = 0; t <= max; t += step) + refine(t); + + step /= 2; + while (step > tolerance) { + if (!refine(minT - step) && !refine(minT + step)) + step /= 2; } var pt = Curve.evaluate(values, minT, true, 0); return new CurveLocation(this, minT, pt, null, point.getDistance(pt)); diff --git a/src/path/Path.js b/src/path/Path.js index 1b29d84b..083d6477 100644 --- a/src/path/Path.js +++ b/src/path/Path.js @@ -1539,6 +1539,7 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{ * the specified point */ getNearestLocation: function(point) { + point = this._matrix.inverseTransform(Point.read(arguments)); var curves = this.getCurves(), minDist = Infinity, minLoc = null; @@ -1553,6 +1554,7 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{ }, _getNearestLocation: function(point) { + point = this._matrix.inverseTransform(Point.read(arguments)); var curves = this.getCurves(), minDist = Infinity, minLoc = null;