Rename Curve#getParameter(length) -> Curve#getParameterAt(offset)

This commit is contained in:
Jürg Lehni 2011-07-06 23:13:38 +02:00
parent 778a161ba0
commit b13c7622c8
5 changed files with 26 additions and 27 deletions

View file

@ -39,8 +39,8 @@
} else { } else {
for (var i = 0, pos = 0; i <= num; i++, pos += step) { for (var i = 0, pos = 0; i <= num; i++, pos += step) {
var t = iteratively var t = iteratively
? curve.getParameter(step, prev) ? curve.getParameterAt(step, prev)
: curve.getParameter(pos); : curve.getParameterAt(pos);
var point = curve.getPoint(t); var point = curve.getPoint(t);
var circle = new Path.Circle(point, step / 2); var circle = new Path.Circle(point, step / 2);
circle.strokeColor = 'red'; circle.strokeColor = 'red';

View file

@ -253,16 +253,17 @@ var Curve = this.Curve = Base.extend(/** @lends Curve# */{
}, },
// PORT: Add support for start parameter to Sg // PORT: Add support for start parameter to Sg
// DOCS: document Curve#getParameter(length, start) // PORT: Rename #getParameter(length) -> #getParameterAt(offset)
// DOCS: Document #getParameter(length, start)
/** /**
* @param {Number} length * @param {Number} offset
* @param {Number} [start] * @param {Number} [start]
* @return {Number} * @return {Number}
*/ */
getParameter: function(length, start) { getParameterAt: function(offset, start) {
var args = this.getValues(); var args = this.getValues();
args.push(length, start !== undefined ? start : length < 0 ? 1 : 0); args.push(offset, start !== undefined ? start : offset < 0 ? 1 : 0);
return Curve.getParameter.apply(Curve, args); return Curve.getParameterAt.apply(Curve, args);
}, },
/** /**
@ -606,16 +607,16 @@ var Curve = this.Curve = Base.extend(/** @lends Curve# */{
return Numerical.integrate(ds, a, b, getIterations(a, b)); return Numerical.integrate(ds, a, b, getIterations(a, b));
}, },
getParameter: function(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, getParameterAt: function(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y,
length, start) { offset, start) {
if (length == 0) if (offset == 0)
return start; return start;
// See if we're going forward or backward, and handle cases // See if we're going forward or backward, and handle cases
// differently // differently
var forward = length > 0, var forward = offset > 0,
a = forward ? start : 0, a = forward ? start : 0,
b = forward ? 1 : start, b = forward ? 1 : start,
length = Math.abs(length), offset = Math.abs(offset),
// Use integrand to calculate both range length and part // Use integrand to calculate both range length and part
// lengths in f(t) below. // lengths in f(t) below.
ds = getLengthIntegrand( ds = getLengthIntegrand(
@ -623,25 +624,23 @@ var Curve = this.Curve = Base.extend(/** @lends Curve# */{
// Get length of total range // Get length of total range
rangeLength = Numerical.integrate(ds, a, b, rangeLength = Numerical.integrate(ds, a, b,
getIterations(a, b)); getIterations(a, b));
if (length >= rangeLength) if (offset >= rangeLength)
return forward ? b : a; return forward ? b : a;
// Use length / rangeLength for an initial guess for t, to // Use offset / rangeLength for an initial guess for t, to
// bring us closer: // bring us closer:
var guess = length / rangeLength, var guess = offset / rangeLength,
len = 0; length = 0;
// Iteratively calculate curve range lengths, and add them up, // Iteratively calculate curve range lengths, and add them up,
// using integration precision depending on the size of the // using integration precision depending on the size of the
// range. This is much faster and also more precise than not // range. This is much faster and also more precise than not
// modifing start and calculating total length each time. // modifing start and calculating total length each time.
function f(t) { function f(t) {
var count = getIterations(start, t); var count = getIterations(start, t);
if (start < t) { length += start < t
len += Numerical.integrate(ds, start, t, count); ? Numerical.integrate(ds, start, t, count)
} else { : -Numerical.integrate(ds, t, start, count);
len -= Numerical.integrate(ds, t, start, count);
}
start = t; start = t;
return len - length; return length - offset;
} }
return Numerical.findRoot(f, ds, return Numerical.findRoot(f, ds,
forward ? a + guess : b - guess, // Initial guess for x forward ? a + guess : b - guess, // Initial guess for x
@ -832,5 +831,5 @@ var Curve = this.Curve = Base.extend(/** @lends Curve# */{
getNearestPoint: function(point) { getNearestPoint: function(point) {
return this.getNearestLocation(point).getPoint(); return this.getNearestLocation(point).getPoint();
} }
} };
}); });

View file

@ -136,7 +136,7 @@ CurveLocation = Base.extend(/** @lends CurveLocation# */{
*/ */
getParameter: function() { getParameter: function() {
if (this._parameter == null && this._curve && this._point) if (this._parameter == null && this._curve && this._point)
this._parameter = this._curve.getParameter(this._point); this._parameter = this._curve.getParameterAt(this._point);
return this._parameter; return this._parameter;
}, },

View file

@ -960,7 +960,7 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{
if (length >= offset) { if (length >= offset) {
// Found the segment within which the length lies // Found the segment within which the length lies
return new CurveLocation(curve, return new CurveLocation(curve,
curve.getParameter(offset - start)); curve.getParameterAt(offset - start));
} }
} }
// It may be that through impreciseness of getLength, that the end // It may be that through impreciseness of getLength, that the end

View file

@ -25,7 +25,7 @@ test('path.length', function() {
var length = path.length; var length = path.length;
compareNumbers(length, 172.10122680664062); compareNumbers(length, 172.10122680664062);
var param = path.curves[0].getParameter(length / 4); var param = path.curves[0].getParameterAt(length / 4);
compareNumbers(param, 0.2255849553116685); compareNumbers(param, 0.2255849553116685);
}); });
@ -35,6 +35,6 @@ test('curve.getParameter with straight curve', function() {
path.lineTo(500, 500); path.lineTo(500, 500);
var curve = path.curves[0]; var curve = path.curves[0];
var length = curve.length; var length = curve.length;
var t = curve.getParameter(length / 3); var t = curve.getParameterAt(length / 3);
compareNumbers(t, 0.3869631475722452); compareNumbers(t, 0.3869631475722452);
}); });