diff --git a/src/path/CompoundPath.js b/src/path/CompoundPath.js index a7592823..f1d00990 100644 --- a/src/path/CompoundPath.js +++ b/src/path/CompoundPath.js @@ -153,6 +153,21 @@ var CompoundPath = this.CompoundPath = PathItem.extend(/** @lends CompoundPath# return last && last.getFirstCurve(); }, + /** + * The area of the path in square points. Self-intersecting paths can + * contain sub-areas that cancel each other out. + * + * @type Number + * @bean + */ + getArea: function() { + var children = this._children, + area = 0; + for (var i = 0, l = children.length; i < l; i++) + area += children[i].getArea(); + return area; + }, + getPathData: function(/* precision */) { var children = this._children, paths = []; diff --git a/src/path/Curve.js b/src/path/Curve.js index 53f04e58..24c74bad 100644 --- a/src/path/Curve.js +++ b/src/path/Curve.js @@ -267,6 +267,10 @@ var Curve = this.Curve = Base.extend(/** @lends Curve# */{ return length; }, + getArea: function() { + return Curve.getArea(this.getValues()); + }, + getPart: function(from, to) { return new Curve(Curve.getPart(this.getValues(), from, to)); }, @@ -986,6 +990,20 @@ new function() { // Scope for methods that require numerical integration return Numerical.integrate(ds, a, b, getIterations(a, b)); }, + getArea: function(v) { + var p1x = v[0], p1y = v[1], + c1x = v[2], c1y = v[3], + c2x = v[4], c2y = v[5], + p2x = v[6], p2y = v[7]; + // http://objectmix.com/graphics/133553-area-closed-bezier-curve.html + return 3 / 10 * c1y * p1x - 3 / 20 * c1y * c2x + - 3 / 20 * c1y * p2x - 3 / 10 * p1y * c1x + - 3 / 20 * p1y * c2x - 1 / 20 * p1y * p2x + + 3 / 20 * c2y * p1x + 3 / 20 * c2y * c1x + - 3 / 10 * c2y * p2x + 1 / 20 * p2y * p1x + + 3 / 20 * p2y * c1x + 3 / 10 * p2y * c2x; + }, + getParameterAt: function(v, offset, start) { if (offset == 0) return start; diff --git a/src/path/Path.js b/src/path/Path.js index c7ba2f4c..ac2a2a66 100644 --- a/src/path/Path.js +++ b/src/path/Path.js @@ -1226,7 +1226,7 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{ }, /** - * The length of the perimeter of the path. + * The approximate length of the path in points. * * @type Number * @bean @@ -1241,6 +1241,21 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{ return this._length; }, + /** + * The area of the path in square points. Self-intersecting paths can + * contain sub-areas that cancel each other out. + * + * @type Number + * @bean + */ + getArea: function() { + var curves = this.getCurves(); + var area = 0; + for (var i = 0, l = curves.length; i < l; i++) + area += curves[i].getArea(); + return area; + }, + _getOffset: function(location) { var index = location && location.getIndex(); if (index != null) {