diff --git a/examples/Scripts/CurveTimeParameterization.html b/examples/Scripts/CurveTimeParameterization.html index 56372a47..aa06d299 100644 --- a/examples/Scripts/CurveTimeParameterization.html +++ b/examples/Scripts/CurveTimeParameterization.html @@ -41,7 +41,7 @@ var t = iteratively ? curve.getParameterAt(step, prev) : curve.getParameterAt(pos); - var point = curve.getPoint(t); + var point = curve.getPointAt(t, true); var circle = new Path.Circle(point, step / 2); circle.strokeColor = 'red'; if (remove) diff --git a/src/path/Curve.js b/src/path/Curve.js index 6242bc87..b7e154cb 100644 --- a/src/path/Curve.js +++ b/src/path/Curve.js @@ -262,71 +262,6 @@ var Curve = this.Curve = Base.extend(/** @lends Curve# */{ && this._segment2._handleIn.isZero(); }, - // DOCS: Document #getParameterAt(offset, start) - // DOCS: Document #getParameterOf(point) - // DOCS: Document #getLocationAt(offset, isParameter) - // DOCS: Document #getLocationOf(point) - /** - * @param {Number} offset - * @param {Number} [start] - * @return {Number} - */ - getParameterAt: function(offset, start) { - return Curve.getParameterAt(this.getValues(), offset, - start !== undefined ? start : offset < 0 ? 1 : 0); - }, - - /** - * @param {Point} point - * @return {Number} - */ - getParameterOf: function(point) { - point = Point.read(arguments); - 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 ? new CurveLocation(this, t) : null; - }, - - /** - * Returns the point on the curve at the specified position. - * - * @param {Number} parameter the position at which to find the point as - * a value between {@code 0} and {@code 1}. - * @return {Point} - */ - getPoint: function(parameter) { - return Curve.evaluate(this.getValues(), parameter, 0); - }, - - /** - * Returns the tangent point on the curve at the specified position. - * - * @param {Number} parameter the position at which to find the tangent - * point as a value between {@code 0} and {@code 1}. - */ - getTangent: function(parameter) { - return Curve.evaluate(this.getValues(), parameter, 1); - }, - - /** - * Returns the normal point on the curve at the specified position. - * - * @param {Number} parameter the position at which to find the normal - * point as a value between {@code 0} and {@code 1}. - */ - getNormal: function(parameter) { - return Curve.evaluate(this.getValues(), parameter, 2); - }, - getIntersections: function(curve) { return Curve._addIntersections(this.getValues(), curve.getValues(), this, []); @@ -342,15 +277,15 @@ var Curve = this.Curve = Base.extend(/** @lends Curve# */{ crossings = 0; for (var i = 0; i < count; i++) { var t = roots[i]; - if (t >= 0 && t < 1 && Curve.evaluate(vals, t, 0).x > point.x) { + if (t >= 0 && t < 1 && Curve.evaluate(vals, t, true, 0).x > point.x) { // If we're close to 0 and are not changing y-direction from the // previous curve, do not count this root, as we're merely // 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 + && Curve.evaluate(this.getPrevious().getValues(), 1, true, 1).y + * Curve.evaluate(vals, t, true, 1).y >= /*#=*/ Numerical.TOLERANCE) continue; crossings++; @@ -486,8 +421,9 @@ statics: { ]; }, - evaluate: function(v, t, type) { - var p1x = v[0], p1y = v[1], + evaluate: function(v, offset, isParameter, type) { + var t = isParameter ? offset : Curve.getParameterAt(v, offset, 0), + p1x = v[0], p1y = v[1], c1x = v[2], c1y = v[3], c2x = v[4], c2y = v[5], p2x = v[6], p2y = v[7], @@ -752,7 +688,85 @@ statics: { } return locations; } -}}, Base.each(['getBounds', 'getStrokeBounds', 'getHandleBounds', 'getRoughBounds'], +}}, Base.each(['getPoint', 'getTangent', 'getNormal'], + // Note: Although Curve.getBounds() exists, we are using Path.getBounds() to + // determine the bounds of Curve objects with defined segment1 and segment2 + // values Curve.getBounds() can be used directly on curve arrays, without + // the need to create a Curve object first, as required by the code that + // finds path interesections. + function(name, index) { + this[name + 'At'] = function(offset, isParameter) { + return Curve.evaluate(this.getValues(), offset, isParameter, index); + }; + // Deprecated and undocumented, but keep around for now. + // TODO: Remove once enough time has passed (28.01.2013) + this[name] = function(parameter) { + return Curve.evaluate(this.getValues(), parameter, true, index); + }; + }, +/** @lends Curve# */{ + // DOCS: Document #getParameterAt(offset, start) + // DOCS: Document #getParameterOf(point) + // DOCS: Document #getLocationAt(offset, isParameter) + // DOCS: Document #getLocationOf(point) + /** + * @param {Number} offset + * @param {Number} [start] + * @return {Number} + */ + getParameterAt: function(offset, start) { + return Curve.getParameterAt(this.getValues(), offset, + start !== undefined ? start : offset < 0 ? 1 : 0); + }, + + /** + * @param {Point} point + * @return {Number} + */ + getParameterOf: function(point) { + point = Point.read(arguments); + 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 ? new CurveLocation(this, t) : null; + } + + /** + * Returns the point on the curve at the specified position. + * + * @name Curve#getPointAt + * @function + * @param {Number} parameter the position at which to find the point as + * a value between {@code 0} and {@code 1}. + * @return {Point} + */ + + /** + * Returns the tangent point on the curve at the specified position. + * + * @name Curve#getTangentAt + * @function + * @param {Number} parameter the position at which to find the tangent + * point as a value between {@code 0} and {@code 1}. + */ + + /** + * Returns the normal point on the curve at the specified position. + * + * @name Curve#getNormalAt + * @function + * @param {Number} parameter the position at which to find the normal + * point as a value between {@code 0} and {@code 1}. + */ +}), Base.each(['getBounds', 'getStrokeBounds', 'getHandleBounds', 'getRoughBounds'], // Note: Although Curve.getBounds() exists, we are using Path.getBounds() to // determine the bounds of Curve objects with defined segment1 and segment2 // values Curve.getBounds() can be used directly on curve arrays, without @@ -1069,7 +1083,7 @@ new function() { // Scope for methods that require numerical integration minPoint; // There are always roots, since we add [0, 1] above. for (var i = 0; i < roots.length; i++) { - var pt = this.getPoint(roots[i]), + var pt = this.getPointAt(roots[i], true), dist = point.getDistance(pt, true); // We're comparing squared distances if (dist < minDist) { diff --git a/src/path/CurveLocation.js b/src/path/CurveLocation.js index 1b90fede..358de957 100644 --- a/src/path/CurveLocation.js +++ b/src/path/CurveLocation.js @@ -24,7 +24,7 @@ * {@link Path#curves} array is also provided. * * The class is in use in many places, such as - * {@link Path#getLocationAt(offset)}, + * {@link Path#getLocationAt(offset, isParameter)}, * {@link Path#getLocationOf(point)}, * {@link Path#getNearestLocation(point), * {@link PathItem#getIntersections(path)}, @@ -173,7 +173,7 @@ CurveLocation = Base.extend(/** @lends CurveLocation# */{ getPoint: function() { if (!this._point && this._parameter != null) { var curve = this.getCurve(); - this._point = curve && curve.getPoint(this._parameter); + this._point = curve && curve.getPointAt(this._parameter, true); } return this._point; }, @@ -187,7 +187,7 @@ CurveLocation = Base.extend(/** @lends CurveLocation# */{ getTangent: function() { var parameter = this.getParameter(), curve = this.getCurve(); - return parameter != null && curve && curve.getTangent(parameter); + return parameter != null && curve && curve.getTangentAt(parameter, true); }, /** @@ -199,7 +199,7 @@ CurveLocation = Base.extend(/** @lends CurveLocation# */{ getNormal: function() { var parameter = this.getParameter(), curve = this.getCurve(); - return parameter != null && curve && curve.getNormal(parameter); + return parameter != null && curve && curve.getNormalAt(parameter, true); }, /** diff --git a/src/path/Path.js b/src/path/Path.js index d8a45b79..de83135a 100644 --- a/src/path/Path.js +++ b/src/path/Path.js @@ -2055,8 +2055,8 @@ statics: { } function addBevelJoin(curve, t) { - var point = curve.getPoint(t), - normal = curve.getNormal(t).normalize(radius); + var point = curve.getPointAt(t, true), + normal = curve.getNormalAt(t, true).normalize(radius); add(point.add(normal)); add(point.subtract(normal)); } @@ -2075,9 +2075,9 @@ statics: { } else if (join == 'miter') { var curve2 = segment.getCurve(), curve1 = curve2.getPrevious(), - point = curve2.getPoint(0), - normal1 = curve1.getNormal(1).normalize(radius), - normal2 = curve2.getNormal(0).normalize(radius), + point = curve2.getPointAt(0, true), + normal1 = curve1.getNormalAt(1, true).normalize(radius), + normal2 = curve2.getNormalAt(0, true).normalize(radius), // Intersect the two lines line1 = new Line(point.subtract(normal1), Point.create(-normal1.y, normal1.x)), @@ -2102,8 +2102,8 @@ statics: { case 'square': // Calculate the corner points of butt and square caps var curve = segment.getCurve(), - point = curve.getPoint(t), - normal = curve.getNormal(t).normalize(radius); + point = curve.getPointAt(t, true), + normal = curve.getNormalAt(t, true).normalize(radius); // For square caps, we need to step away from point in the // direction of the tangent, which is the rotated normal if (cap === 'square') diff --git a/src/path/PathFlattener.js b/src/path/PathFlattener.js index bc393c06..91bf8d5b 100644 --- a/src/path/PathFlattener.js +++ b/src/path/PathFlattener.js @@ -116,7 +116,7 @@ var PathFlattener = Base.extend({ evaluate: function(offset, type) { var param = this.getParameterAt(offset); - return Curve.evaluate(this.curves[param.index], param.value, type); + return Curve.evaluate(this.curves[param.index], param.value, true, type); }, drawPart: function(ctx, from, to) {