diff --git a/src/item/Shape.js b/src/item/Shape.js index 8b17d112..ae2bda56 100644 --- a/src/item/Shape.js +++ b/src/item/Shape.js @@ -84,7 +84,7 @@ var Shape = this.Shape = Item.extend(/** @lends Shape# */{ _hitTest: function(point, options) { if (this.hasFill() && this.contains(point)) return new HitResult('fill', this); - // TODO: Implement stokre! + // TODO: Implement stroke! }, statics: { diff --git a/src/path/Curve.js b/src/path/Curve.js index a61f3a88..5dd6be5f 100644 --- a/src/path/Curve.js +++ b/src/path/Curve.js @@ -288,7 +288,7 @@ var Curve = this.Curve = Base.extend(/** @lends Curve# */{ getIntersections: function(curve) { return Curve.getIntersections(this.getValues(), curve.getValues(), - this, []); + this, curve, []); }, getCrossings: function(point, roots) { @@ -716,7 +716,7 @@ statics: { // We need to provide the original left curve reference to the // #getIntersections() calls as it is required to create the resulting // CurveLocation objects. - getIntersections: function(v1, v2, curve, locations) { + getIntersections: function(v1, v2, curve1, curve2, locations) { var bounds1 = this.getBounds(v1), bounds2 = this.getBounds(v2); /*#*/ if (options.debug) { @@ -764,7 +764,8 @@ statics: { // Passing null for parameter leads to lazy determination // of parameter values in CurveLocation#getParameter() // only once they are requested. - locations.push(new CurveLocation(curve, null, point)); + locations.push(new CurveLocation(curve1, null, point, + curve2)); } } else { // Subdivide both curves, and see if they intersect. @@ -772,7 +773,7 @@ statics: { v2s = this.subdivide(v2); for (var i = 0; i < 2; i++) for (var j = 0; j < 2; j++) - this.getIntersections(v1s[i], v2s[j], curve, locations); + this.getIntersections(v1s[i], v2s[j], curve1, locations); } } return locations; @@ -1233,7 +1234,8 @@ new function() { // Scope for methods that require numerical integration minPoint = pt; } } - return new CurveLocation(this, minT, minPoint, Math.sqrt(minDist)); + return new CurveLocation(this, minT, minPoint, null, + Math.sqrt(minDist)); }, getNearestPoint: function(point) { diff --git a/src/path/CurveLocation.js b/src/path/CurveLocation.js index e6490d2c..52022bc5 100644 --- a/src/path/CurveLocation.js +++ b/src/path/CurveLocation.js @@ -36,7 +36,7 @@ var CurveLocation = this.CurveLocation = Base.extend(/** @lends CurveLocation# * * @param {Number} parameter * @param {Point} point */ - initialize: function(curve, parameter, point, distance) { + initialize: function(curve, parameter, point, _otherCurve, _distance) { // Define this CurveLocation's unique id. this._id = CurveLocation._id = (CurveLocation._id || 0) + 1; this._curve = curve; @@ -47,7 +47,8 @@ var CurveLocation = this.CurveLocation = Base.extend(/** @lends CurveLocation# * this._segment2 = curve._segment2; this._parameter = parameter; this._point = point; - this._distance = distance; + this._otherCurve = _otherCurve; + this._distance = _distance; }, /** @@ -60,9 +61,9 @@ var CurveLocation = this.CurveLocation = Base.extend(/** @lends CurveLocation# * if (!this._segment) { var curve = this.getCurve(), parameter = this.getParameter(); - if (parameter == 0) { + if (parameter === 0) { this._segment = curve._segment1; - } else if (parameter == 1) { + } else if (parameter === 1) { this._segment = curve._segment2; } else if (parameter == null) { return null; @@ -97,6 +98,26 @@ var CurveLocation = this.CurveLocation = Base.extend(/** @lends CurveLocation# * return this._curve; }, + /** + * The curve location on the intersecting curve, if this location is the + * result of a call to {@link PathItem#getIntersections(path)} / + * {@link Curve#getIntersections(curve)}. + * + * @type CurveLocation + * @bean + */ + 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 + intersection._intersection = this; + } + return intersection; + }, + /** * The path this curve belongs to, if any. * diff --git a/src/path/PathItem.js b/src/path/PathItem.js index 7f703fe7..f3f3e43b 100644 --- a/src/path/PathItem.js +++ b/src/path/PathItem.js @@ -69,10 +69,11 @@ var PathItem = this.PathItem = Item.extend(/** @lends PathItem# */{ for (var i = 0; i < length2; i++) values2[i] = curves2[i].getValues(); for (var i = 0, l = curves1.length; i < l; i++) { - var curve = curves1[i], - values1 = curve.getValues(); + var curve1 = curves1[i], + values1 = curve1.getValues(); for (var j = 0; j < length2; j++) - Curve.getIntersections(values1, values2[j], curve, locations); + Curve.getIntersections(values1, values2[j], curve1, curves2[j], + locations); } return locations; },