From a854c55914cbb05fa9229ba401216bf07a527fdc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ju=CC=88rg=20Lehni?= <juerg@scratchdisk.com>
Date: Sat, 3 Jan 2015 01:46:22 +0100
Subject: [PATCH] Split off code from Path.Clockwise() into Curve.getEdgeSum()

---
 src/path/Curve.js | 14 +++++++++++++-
 src/path/Path.js  | 15 +++------------
 2 files changed, 16 insertions(+), 13 deletions(-)

diff --git a/src/path/Curve.js b/src/path/Curve.js
index 7ae40ef4..759d1313 100644
--- a/src/path/Curve.js
+++ b/src/path/Curve.js
@@ -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
diff --git a/src/path/Path.js b/src/path/Path.js
index b2d689cc..1bafce3a 100644
--- a/src/path/Path.js
+++ b/src/path/Path.js
@@ -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;
     },