Use parameter on both curves in CurveLocation objects instead of Point in #getIntersection()

This commit is contained in:
hkrish 2013-06-08 21:37:43 +05:30
parent 99e58fa449
commit d3405115c1
2 changed files with 45 additions and 27 deletions

View file

@ -1024,13 +1024,13 @@ new function() { // Scope for methods that require numerical integration
}
};
}, new function() { // Scope for intersection using bezier fat-line clipping
function addLocation(locations, curve1, parameter, point, curve2) {
function addLocation(locations, curve1, parameter, point, curve2, parameter2) {
// Avoid duplicates when hitting segments (closed paths too)
var first = locations[0],
last = locations[locations.length - 1];
if ((!first || !point.equals(first._point))
&& (!last || !point.equals(last._point)))
locations.push(new CurveLocation(curve1, parameter, point, curve2));
locations.push(new CurveLocation(curve1, parameter, point, curve2, null, parameter2));
}
function addCurveIntersections(v1, v2, curve1, curve2, locations,
@ -1123,31 +1123,37 @@ new function() { // Scope for methods that require numerical integration
// Check if one of the parameter range has converged completely to a
// point. Now things could get only worse if we iterate more for the
// other curve to converge if it hasn't yet happened so.
if (Math.abs(range1[1] - range1[0]) < /*#=*/ Numerical.TOLERANCE) {
if (Math.abs(range1[1] - range1[0]) < /*#=*/ Numerical.TOLERANCE &&
Math.abs(range2[1] - range2[0]) < /*#=*/ Numerical.TOLERANCE) {
var t = (range1[0] + range1[1]) / 2;
var t2 = (range2[0] + range2[1]) / 2;
addLocation(locations, curve1, t,
Curve.evaluate(v1, t, true, 0), curve2);
break;
}
if (Math.abs(range2[1] - range2[0]) < /*#=*/ Numerical.TOLERANCE) {
var t = (range2[0] + range2[1]) / 2;
addLocation(locations, curve2, t,
Curve.evaluate(v2, t, true, 0), curve1);
Curve.evaluate(v1, t, true, 0), curve2, t2);
break;
}
// if (Math.abs(range1[1] - range1[0]) < /*#=*/ Numerical.TOLERANCE) {
// var t = (range1[0] + range1[1]) / 2;
// addLocation(locations, curve1, t,
// Curve.evaluate(v1, t, true, 0), curve2);
// break;
// }
// if (Math.abs(range2[1] - range2[0]) < /*#=*/ Numerical.TOLERANCE) {
// var t = (range2[0] + range2[1]) / 2;
// addLocation(locations, curve2, t,
// Curve.evaluate(v2, t, true, 0), curve1);
// break;
// }
// see if either or both of the curves are flat enough to be treated
// as lines.
var flat1 = Curve.isFlatEnough(part1, /*#=*/ Numerical.TOLERANCE),
flat2 = Curve.isFlatEnough(part2, /*#=*/ Numerical.TOLERANCE);
if (flat1 || flat2) {
(flat1 && flat2
? addLineIntersection
// Use curve line intersection method while specifying
// which curve to be treated as line
: addCurveLineIntersections)(part1, part2,
curve1, curve2, locations, flat1);
break;
}
// if (flat1 || flat2) {
// (flat1 && flat2
// ? addLineIntersection
// // Use curve line intersection method while specifying
// // which curve to be treated as line
// : addCurveLineIntersections)(part1, part2,
// curve1, curve2, locations, flat1);
// break;
// }
}
/*#*/ } else { // !options.fatline
var bounds1 = Curve.getBounds(v1),
@ -1389,7 +1395,7 @@ new function() { // Scope for methods that require numerical integration
addLocation(locations,
flip ? curve2 : curve1,
// The actual intersection point
t, Curve.evaluate(vc, t, true, 0),
t, Curve.evaluate(vc, t, true, 0),
flip ? curve1 : curve2);
}
}

View file

@ -37,7 +37,7 @@ var CurveLocation = Base.extend(/** @lends CurveLocation# */{
* @param {Point} point
*/
initialize: function CurveLocation(curve, parameter, point, _otherCurve,
_distance) {
_distance, _otherParameter) {
// Define this CurveLocation's unique id.
this._id = CurveLocation._id = (CurveLocation._id || 0) + 1;
this._curve = curve;
@ -49,6 +49,7 @@ var CurveLocation = Base.extend(/** @lends CurveLocation# */{
this._parameter = parameter;
this._point = point;
this._otherCurve = _otherCurve;
this._otherParameter = _otherParameter;
this._distance = _distance;
},
@ -110,10 +111,21 @@ var CurveLocation = Base.extend(/** @lends CurveLocation# */{
getIntersection: function() {
var intersection = this._intersection;
if (!intersection && this._otherCurve) {
// _point is always defined for intersection
intersection = this._intersection = new CurveLocation(
this._otherCurve, null, this._point, this);
// Link both ways
if( this._otherParameter ){
// if we have the parameter on the other curve use that for intersection
// rather than the point.
intersection = this._intersection = new CurveLocation(
this._otherCurve, this._otherParameter, null, this);
// Force calculate the other point from the parameter.
// DEBUG: @jlehni - Not sure why we have to do this? Shouldn't it auto-calculate
// upon first access?!
intersection.getPoint();
} else {
// _point is always defined for intersection
intersection = this._intersection = new CurveLocation(
this._otherCurve, null, this._point, this);
// Link both ways
}
intersection._intersection = this;
}
return intersection;