Define CurveLocation#intersection as a way to retrieve the CurveLocation on the intersecting path when using Path#getIntersections().

This commit is contained in:
Jürg Lehni 2013-04-30 18:41:26 -07:00
parent 8d35d92946
commit 69f8bdc560
4 changed files with 37 additions and 13 deletions

View file

@ -84,7 +84,7 @@ var Shape = this.Shape = Item.extend(/** @lends Shape# */{
_hitTest: function(point, options) { _hitTest: function(point, options) {
if (this.hasFill() && this.contains(point)) if (this.hasFill() && this.contains(point))
return new HitResult('fill', this); return new HitResult('fill', this);
// TODO: Implement stokre! // TODO: Implement stroke!
}, },
statics: { statics: {

View file

@ -288,7 +288,7 @@ var Curve = this.Curve = Base.extend(/** @lends Curve# */{
getIntersections: function(curve) { getIntersections: function(curve) {
return Curve.getIntersections(this.getValues(), curve.getValues(), return Curve.getIntersections(this.getValues(), curve.getValues(),
this, []); this, curve, []);
}, },
getCrossings: function(point, roots) { getCrossings: function(point, roots) {
@ -716,7 +716,7 @@ statics: {
// We need to provide the original left curve reference to the // We need to provide the original left curve reference to the
// #getIntersections() calls as it is required to create the resulting // #getIntersections() calls as it is required to create the resulting
// CurveLocation objects. // CurveLocation objects.
getIntersections: function(v1, v2, curve, locations) { getIntersections: function(v1, v2, curve1, curve2, locations) {
var bounds1 = this.getBounds(v1), var bounds1 = this.getBounds(v1),
bounds2 = this.getBounds(v2); bounds2 = this.getBounds(v2);
/*#*/ if (options.debug) { /*#*/ if (options.debug) {
@ -764,7 +764,8 @@ statics: {
// Passing null for parameter leads to lazy determination // Passing null for parameter leads to lazy determination
// of parameter values in CurveLocation#getParameter() // of parameter values in CurveLocation#getParameter()
// only once they are requested. // only once they are requested.
locations.push(new CurveLocation(curve, null, point)); locations.push(new CurveLocation(curve1, null, point,
curve2));
} }
} else { } else {
// Subdivide both curves, and see if they intersect. // Subdivide both curves, and see if they intersect.
@ -772,7 +773,7 @@ statics: {
v2s = this.subdivide(v2); v2s = this.subdivide(v2);
for (var i = 0; i < 2; i++) for (var i = 0; i < 2; i++)
for (var j = 0; j < 2; j++) 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; return locations;
@ -1233,7 +1234,8 @@ new function() { // Scope for methods that require numerical integration
minPoint = pt; minPoint = pt;
} }
} }
return new CurveLocation(this, minT, minPoint, Math.sqrt(minDist)); return new CurveLocation(this, minT, minPoint, null,
Math.sqrt(minDist));
}, },
getNearestPoint: function(point) { getNearestPoint: function(point) {

View file

@ -36,7 +36,7 @@ var CurveLocation = this.CurveLocation = Base.extend(/** @lends CurveLocation# *
* @param {Number} parameter * @param {Number} parameter
* @param {Point} point * @param {Point} point
*/ */
initialize: function(curve, parameter, point, distance) { initialize: function(curve, parameter, point, _otherCurve, _distance) {
// Define this CurveLocation's unique id. // Define this CurveLocation's unique id.
this._id = CurveLocation._id = (CurveLocation._id || 0) + 1; this._id = CurveLocation._id = (CurveLocation._id || 0) + 1;
this._curve = curve; this._curve = curve;
@ -47,7 +47,8 @@ var CurveLocation = this.CurveLocation = Base.extend(/** @lends CurveLocation# *
this._segment2 = curve._segment2; this._segment2 = curve._segment2;
this._parameter = parameter; this._parameter = parameter;
this._point = point; 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) { if (!this._segment) {
var curve = this.getCurve(), var curve = this.getCurve(),
parameter = this.getParameter(); parameter = this.getParameter();
if (parameter == 0) { if (parameter === 0) {
this._segment = curve._segment1; this._segment = curve._segment1;
} else if (parameter == 1) { } else if (parameter === 1) {
this._segment = curve._segment2; this._segment = curve._segment2;
} else if (parameter == null) { } else if (parameter == null) {
return null; return null;
@ -97,6 +98,26 @@ var CurveLocation = this.CurveLocation = Base.extend(/** @lends CurveLocation# *
return this._curve; 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. * The path this curve belongs to, if any.
* *

View file

@ -69,10 +69,11 @@ var PathItem = this.PathItem = Item.extend(/** @lends PathItem# */{
for (var i = 0; i < length2; i++) for (var i = 0; i < length2; i++)
values2[i] = curves2[i].getValues(); values2[i] = curves2[i].getValues();
for (var i = 0, l = curves1.length; i < l; i++) { for (var i = 0, l = curves1.length; i < l; i++) {
var curve = curves1[i], var curve1 = curves1[i],
values1 = curve.getValues(); values1 = curve1.getValues();
for (var j = 0; j < length2; j++) 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; return locations;
}, },