Rename CurveLocation related functions to *At(offset), and add optional isParameter argument that defines whether offset is a length or a description of curve index / curve parameter.

This commit is contained in:
Jürg Lehni 2011-04-27 21:40:52 +01:00
parent ea6cf24aae
commit 6a483fa8be
3 changed files with 37 additions and 33 deletions

View file

@ -61,8 +61,8 @@
var path = lineGroup.children[i], var path = lineGroup.children[i],
l1 = (length1 / lineCount * (i + count / 10)) % length1, l1 = (length1 / lineCount * (i + count / 10)) % length1,
l2 = (length2 / lineCount * (i + count / 10)) % length2; l2 = (length2 / lineCount * (i + count / 10)) % length2;
path.segments[0].point = path1.getLocation(l1).point, path.segments[0].point = path1.getPointAt(l1),
path.segments[1].point = path2.getLocation(l2).point; path.segments[1].point = path2.getPointAt(l2);
} }
count++; count++;
} }

View file

@ -251,7 +251,7 @@
count++; count++;
for(var i = 0, l = boids.length; i < l; i++) { for(var i = 0, l = boids.length; i < l; i++) {
if (groupTogether) { if (groupTogether) {
var point = heartPath.getLocation(((i + count / 30) % l) / l * pathLength).point; var point = heartPath.getPointAt(((i + count / 30) % l) / l * pathLength);
boids[i].arrive(point); boids[i].arrive(point);
} }
boids[i].run(boids); boids[i].run(boids);

View file

@ -210,30 +210,6 @@ var Path = this.Path = PathItem.extend({
return false; return false;
}, },
// TODO: getLocation(point, precision)
getLocation: function(offset) {
var curves = this.getCurves(),
currentLength = 0;
for (var i = 0, l = curves.length; i < l; i++) {
var startLength = currentLength,
curve = curves[i];
currentLength += curve.getLength();
if (currentLength >= length) {
// found the segment within which the length lies
var t = curve.getParameter(length - startLength);
return new CurveLocation(curve, t);
}
}
// TODO: is this the case for paper.js too?
// it may be that through impreciseness of getLength, that the end
// of the curves was missed:
if (length <= this.getLength()) {
var curve = curves[curves.length - 1];
return new CurveLocation(curve, 1);
}
return null;
},
getLength: function() { getLength: function() {
var curves = this.getCurves(); var curves = this.getCurves();
var length = 0; var length = 0;
@ -255,11 +231,39 @@ var Path = this.Path = PathItem.extend({
return null; return null;
}, },
// TODO: getLocationAt(point, precision)
// TODO: Port back renaming and new isParameter argument to Scriptographer
getLocationAt: function(offset, isParameter) {
var curves = this.getCurves(),
length = 0;
if (isParameter) {
// 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);
}
for (var i = 0, l = curves.length; i < l; i++) {
var start = length,
curve = curves[i];
length += curve.getLength();
if (length >= offset) {
// Found the segment within which the length lies
return new CurveLocation(curve,
curve.getParameter(offset - start));
}
}
// It may be that through impreciseness of getLength, that the end
// of the curves was missed:
if (offset <= this.getLength())
return new CurveLocation(curves[curves.length - 1], 1);
return null;
},
/** /**
* Returns the point of the path at the given offset. * Returns the point of the path at the given offset.
*/ */
getPoint: function(offset) { getPointAt: function(offset, isParameter) {
var loc = this.getLocation(offset); var loc = this.getLocationAt(offset, isParameter);
return loc && loc.getPoint(); return loc && loc.getPoint();
}, },
@ -267,16 +271,16 @@ var Path = this.Path = PathItem.extend({
* Returns the tangent to the path at the given offset as a vector * Returns the tangent to the path at the given offset as a vector
* point. * point.
*/ */
getTangent: function(offset) { getTangentAt: function(offset, isParameter) {
var loc = this.getLocation(offset); var loc = this.getLocationAt(offset, isParameter);
return loc && loc.getTangent(); return loc && loc.getTangent();
}, },
/** /**
* Returns the normal to the path at the given offset as a vector point. * Returns the normal to the path at the given offset as a vector point.
*/ */
getNormal: function(offset) { getNormalAt: function(offset, isParameter) {
var loc = this.getLocation(offset); var loc = this.getLocationAt(offset, isParameter);
return loc && loc.getNormal(); return loc && loc.getNormal();
} }
}, new function() { // Scope for drawing }, new function() { // Scope for drawing