mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-03 19:45:44 -05:00
Implement Path#getArea() and CompoundPath#getArea().
This commit is contained in:
parent
80a1129eab
commit
898e216668
3 changed files with 49 additions and 1 deletions
|
@ -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 = [];
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue