Use parameter tolerances in Path#slipt()

Required because CurveLocation objects as returned by Path#getLocationAt() have some imprecision in their #parameter values. Fixes #401.
This commit is contained in:
Jürg Lehni 2014-02-15 23:37:41 +01:00
parent 2010fefcbe
commit ad801fa786

View file

@ -1068,15 +1068,17 @@ var Path = PathItem.extend(/** @lends Path# */{
index = arg.index; index = arg.index;
parameter = arg.parameter; parameter = arg.parameter;
} }
if (parameter >= 1) { var tolerance = /*#=*/ Numerical.TOLERANCE;
if (parameter >= 1 - tolerance) {
// t == 1 is the same as t == 0 and index ++ // t == 1 is the same as t == 0 and index ++
index++; index++;
parameter--; parameter--;
} }
var curves = this.getCurves(); var curves = this.getCurves();
paper.console.log(index, parameter);
if (index >= 0 && index < curves.length) { if (index >= 0 && index < curves.length) {
// Only divide curves if we're not on an existing segment already. // Only divide curves if we're not on an existing segment already.
if (parameter > 0) { if (parameter > tolerance) {
// Divide the curve with the index at given parameter. // Divide the curve with the index at given parameter.
// Increase because dividing adds more segments to the path. // Increase because dividing adds more segments to the path.
curves[index++].divide(parameter, true); curves[index++].divide(parameter, true);
@ -1344,13 +1346,13 @@ var Path = PathItem.extend(/** @lends Path# */{
var start = length, var start = length,
curve = curves[i]; curve = curves[i];
length += curve.getLength(); length += curve.getLength();
if (length >= offset) { if (length > offset) {
// Found the segment within which the length lies // Found the segment within which the length lies
return curve.getLocationAt(offset - start); return curve.getLocationAt(offset - start);
} }
} }
// It may be that through impreciseness of getLength, that the end // It may be that through imprecision of getLength, that the end of the
// of the curves was missed: // last curve was missed:
if (offset <= this.getLength()) if (offset <= this.getLength())
return new CurveLocation(curves[curves.length - 1], 1); return new CurveLocation(curves[curves.length - 1], 1);
return null; return null;