Find a better naming convention for the various getLocation* methods (*At for offsets / parameters, *Of for points), implement the missing methods on Curve and use them in Path.

This commit is contained in:
Jürg Lehni 2012-12-27 21:08:03 +01:00
parent 231369fa01
commit 1b539301ad
3 changed files with 43 additions and 28 deletions

View file

@ -262,7 +262,10 @@ var Curve = this.Curve = Base.extend(/** @lends Curve# */{
&& this._segment2._handleIn.isZero();
},
// DOCS: Document #getParameter(length, start)
// DOCS: Document #getParameterAt(offset, start)
// DOCS: Document #getParameterOf(point)
// DOCS: Document #getLocationAt(offset, isParameter)
// DOCS: Document #getLocationOf(point)
/**
* @param {Number} offset
* @param {Number} [start]
@ -282,6 +285,17 @@ var Curve = this.Curve = Base.extend(/** @lends Curve# */{
return Curve.getParameterOf(this.getValues(), point.x, point.y);
},
getLocationAt: function(offset, isParameter) {
if (!isParameter)
offset = this.getParameterAt(offset);
return new CurveLocation(this, offset);
},
getLocationOf: function(point) {
var t = this.getParameterOf.apply(this, arguments);
return t != null ? CurveLocation(this, t) : null;
},
/**
* Returns the point on the curve at the specified position.
*
@ -334,9 +348,10 @@ var Curve = this.Curve = Base.extend(/** @lends Curve# */{
// touching a tip. Passing 1 for Curve.evaluate()'s type means
// we're calculating tangents, and then check their y-slope for
// a change of direction:
if (t < /*#=*/ Numerical.TOLERANCE && Curve.evaluate(
this.getPrevious().getValues(), 1, 1).y
* Curve.evaluate(vals, t, 1).y >= /*#=*/ Numerical.TOLERANCE)
if (t < /*#=*/ Numerical.TOLERANCE
&& Curve.evaluate(this.getPrevious().getValues(), 1, 1).y
* Curve.evaluate(vals, t, 1).y
>= /*#=*/ Numerical.TOLERANCE)
continue;
crossings++;
}
@ -344,7 +359,6 @@ var Curve = this.Curve = Base.extend(/** @lends Curve# */{
return crossings;
},
// TODO: getLocation
// TODO: adjustThroughPoint
/**
@ -563,10 +577,9 @@ statics: {
var min = v.slice(0, 2),
max = min.slice(0), // clone
roots = new Array(2);
for (var i = 0; i < 2; i++) {
for (var i = 0; i < 2; i++)
Curve._addBounds(v[i], v[i + 2], v[i + 4], v[i + 6],
i, 0, min, max, roots);
}
return Rectangle.create(min[0], min[1], max[0] - min[0], max[1] - min[1]);
},
@ -979,7 +992,7 @@ new function() { // Scope for methods that require numerical integration
}
return {
getNearestLocation: function(point) {
getNearestLocationOf: function(point) {
// NOTE: If we allow #matrix on Path, we need to inverse-transform
// point here first.
// point = this._matrix.inverseTransform(point);
@ -1003,8 +1016,8 @@ new function() { // Scope for methods that require numerical integration
return new CurveLocation(this, minT, minPoint, Math.sqrt(minDist));
},
getNearestPoint: function(point) {
return this.getNearestLocation(point).getPoint();
getNearestPointOf: function(point) {
return this.getNearestLocationOf(point).getPoint();
}
};
});

View file

@ -24,8 +24,11 @@
* {@link Path#curves} array is also provided.
*
* The class is in use in many places, such as
* {@link Path#getLocationAt(offset)}, Path#getNearestLocation(point),
* {@link PathItem#getIntersections(path)}, etc.
* {@link Path#getLocationAt(offset)},
* {@link Path#getLocationOf(point)},
* {@link Path#getNearestLocationOf(point),
* {@link PathItem#getIntersections(path)},
* etc.
*/
CurveLocation = Base.extend(/** @lends CurveLocation# */{
// DOCS: CurveLocation class description: add these back when the mentioned

View file

@ -988,13 +988,13 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{
return null;
},
getLocation: function(point) {
getLocationOf: function(point) {
point = Point.read(arguments);
var curves = this.getCurves();
for (var i = 0, l = curves.length; i < l; i++) {
var curve = curves[i];
var t = curve.getParameterOf(point);
if (t != null)
return new CurveLocation(curve, t);
var loc = curves[i].getLocationOf(point);
if (loc != null)
return loc;
}
return null;
},
@ -1014,7 +1014,7 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{
// offset consists of curve index and curve parameter, before and
// after the fractional digit.
var index = ~~offset; // = Math.floor()
return new CurveLocation(curves[index], offset - index);
return curves[index].getLocationAt(offset - index, true);
}
for (var i = 0, l = curves.length; i < l; i++) {
var start = length,
@ -1022,8 +1022,7 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{
length += curve.getLength();
if (length >= offset) {
// Found the segment within which the length lies
return new CurveLocation(curve,
curve.getParameterAt(offset - start));
return curve.getLocationAt(offset - start);
}
}
// It may be that through impreciseness of getLength, that the end
@ -1227,18 +1226,18 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{
/**
* Returns the nearest location on the path to the specified point.
*
* @name Path#getNearestLocation
* @name Path#getNearestLocationOf
* @function
* @param point {Point} The point for which we search the nearest location
* @return {CurveLocation} The location on the path that's the closest to
* the specified point
*/
getNearestLocation: function(point) {
getNearestLocationOf: function(point) {
var curves = this.getCurves(),
minDist = Infinity,
minLoc = null;
for (var i = 0, l = curves.length; i < l; i++) {
var loc = curves[i].getNearestLocation(point);
var loc = curves[i].getNearestLocationOf(point);
if (loc._distance < minDist) {
minDist = loc._distance;
minLoc = loc;
@ -1250,14 +1249,14 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{
/**
* Returns the nearest point on the path to the specified point.
*
* @name Path#getNearestPoint
* @name Path#getNearestPointOf
* @function
* @param point {Point} The point for which we search the nearest point
* @return {Point} The point on the path that's the closest to the specified
* point
*/
getNearestPoint: function(point) {
return this.getNearestLocation(point).getPoint();
getNearestPointOf: function(point) {
return this.getNearestLocationOf(point).getPoint();
},
contains: function(point) {
@ -1325,7 +1324,7 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{
}
// If we're querying for stroke, perform that before fill
if (options.stroke && radius > 0)
loc = this.getNearestLocation(point);
loc = this.getNearestLocationOf(point);
// Don't process loc yet, as we also need to query for stroke after fill
// in some cases. Simply skip fill query if we already have a matching
// stroke.
@ -1334,7 +1333,7 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{
return new HitResult('fill', this);
// Now query stroke if we haven't already
if (!loc && options.stroke && radius > 0)
loc = this.getNearestLocation(point);
loc = this.getNearestLocationOf(point);
if (loc && loc._distance <= radius)
// TODO: Do we need to transform the location back to the coordinate
// system of the DOM level on which the inquiry was started?