Split off code from Path.Clockwise() into Curve.getEdgeSum()

This commit is contained in:
Jürg Lehni 2015-01-03 01:46:22 +01:00
parent 80e1a54171
commit a854c55914
2 changed files with 16 additions and 13 deletions

View file

@ -1,4 +1,4 @@
/*
/*
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
* http://paperjs.org/
*
@ -671,6 +671,18 @@ statics: {
+ 1.5 * p2y * c1x + 3.0 * p2y * c2x) / 10;
},
getEdgeSum: function(v) {
// Method derived from:
// http://stackoverflow.com/questions/1165647
// We treat the curve points and handles as the outline of a polygon of
// which we determine the orientation using the method of calculating
// the sum over the edges. This will work even with non-convex polygons,
// telling you whether it's mostly clockwise
return (v[0] - v[2]) * (v[3] + v[1])
+ (v[2] - v[4]) * (v[5] + v[3])
+ (v[4] - v[6]) * (v[7] + v[5]);
},
getBounds: function(v) {
var min = v.slice(0, 2), // Start with values of point1
max = min.slice(), // clone

View file

@ -2612,19 +2612,10 @@ statics: {
*/
isClockwise: function(segments) {
var sum = 0;
// Method derived from:
// http://stackoverflow.com/questions/1165647
// We treat the curve points and handles as the outline of a polygon of
// which we determine the orientation using the method of calculating
// the sum over the edges. This will work even with non-convex polygons,
// telling you whether it's mostly clockwise
// TODO: Check if this works correctly for all open paths.
for (var i = 0, l = segments.length; i < l; i++) {
var v = Curve.getValues(
segments[i], segments[i + 1 < l ? i + 1 : 0]);
for (var j = 2; j < 8; j += 2)
sum += (v[j - 2] - v[j]) * (v[j + 1] + v[j - 1]);
}
for (var i = 0, l = segments.length; i < l; i++)
sum += Curve.getEdgeSum(Curve.getValues(
segments[i], segments[i + 1 < l ? i + 1 : 0]));
return sum > 0;
},