Implement Path#getArea() and CompoundPath#getArea().

This commit is contained in:
Jürg Lehni 2013-04-25 17:37:19 -07:00
parent 80a1129eab
commit 898e216668
3 changed files with 49 additions and 1 deletions
src/path

View file

@ -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;