diff --git a/src/area.js b/src/area.js index 109efb4..9c6e1e3 100644 --- a/src/area.js +++ b/src/area.js @@ -1,14 +1,19 @@ +// Shoelace formula, relative to the first vertex. export default function(polygon) { - var i = -1, - n = polygon.length, - a, - b = polygon[n - 1], - area = 0; + if (!polygon.length) return 0; - while (++i < n) { - a = b; - b = polygon[i]; - area += a[1] * b[0] - a[0] * b[1]; + const x0 = polygon[0][0]; + const y0 = polygon[0][1]; + let ax = 0; + let ay = 0; + let area = 0; + + for (let i = 1; i < polygon.length; ++i) { + const bx = polygon[i][0] - x0; + const by = polygon[i][1] - y0; + area += ay * bx - ax * by; + ax = bx; + ay = by; } return area / 2; diff --git a/src/centroid.js b/src/centroid.js index 6c8ece1..c81ee29 100644 --- a/src/centroid.js +++ b/src/centroid.js @@ -1,20 +1,25 @@ +// Shoelace formula, relative to the first vertex. export default function(polygon) { - var i = -1, - n = polygon.length, - x = 0, - y = 0, - a, - b = polygon[n - 1], - c, - k = 0; + if (!polygon.length) return [NaN, NaN]; - while (++i < n) { - a = b; - b = polygon[i]; - k += c = a[0] * b[1] - b[0] * a[1]; - x += (a[0] + b[0]) * c; - y += (a[1] + b[1]) * c; + const x0 = polygon[0][0]; + const y0 = polygon[0][1]; + let ax = 0; + let ay = 0; + let x = 0; + let y = 0; + let k = 0; + + for (let i = 1; i < polygon.length; ++i) { + const bx = polygon[i][0] - x0; + const by = polygon[i][1] - y0; + const c = ax * by - bx * ay; + k += c; + x += (ax + bx) * c; + y += (ay + by) * c; + ax = bx; + ay = by; } - return k *= 3, [x / k, y / k]; + return k *= 3, [x / k + x0, y / k + y0]; } diff --git a/test/area-test.js b/test/area-test.js index 4718610..a46b7e6 100644 --- a/test/area-test.js +++ b/test/area-test.js @@ -30,3 +30,18 @@ it("polygonArea(polygon) returns the expected value for a very large polygon", ( for (let value = stop - step; value >= 0; value -= step) points.push([value, 0]); assert.strictEqual(polygonArea(points), 1e16 - 5e7); }); + +it("polygonArea(polygon) returns the expected value for a polygon far from the origin", () => { + for (const t of [1e7, 1e8, 1e12]) { + assert.strictEqual(polygonArea([[t, t], [t, t + 1], [t + 1, t + 1], [t + 1, t]]), 1); + } +}); + +it("polygonArea(polygon) returns zero for an empty polygon", () => { + assert.strictEqual(polygonArea([]), 0); +}); + +it("polygonArea(polygon) returns zero for a polygon with fewer than three vertices", () => { + assert.strictEqual(polygonArea([[1, 2]]), 0); + assert.strictEqual(polygonArea([[1, 2], [3, 4]]), 0); +}); diff --git a/test/centroid-test.js b/test/centroid-test.js index e154e1f..16c8aaf 100644 --- a/test/centroid-test.js +++ b/test/centroid-test.js @@ -30,3 +30,18 @@ it("polygonCentroid(polygon) returns the expected value for a very large polygon for (let value = stop - step; value >= 0; value -= step) points.push([value, 0]); assert.deepStrictEqual(polygonCentroid(points), [49999999.75000187, 49999999.75001216]); }); + +it("polygonCentroid(polygon) returns the expected value for a polygon far from the origin", () => { + for (const t of [1e7, 1e8, 1e12]) { + assert.deepStrictEqual(polygonCentroid([[t, t], [t, t + 1], [t + 1, t + 1], [t + 1, t]]), [t + 0.5, t + 0.5]); + } +}); + +it("polygonCentroid(polygon) returns [NaN, NaN] for an empty polygon", () => { + assert.deepStrictEqual(polygonCentroid([]), [NaN, NaN]); +}); + +it("polygonCentroid(polygon) returns [NaN, NaN] for a polygon with fewer than three vertices", () => { + assert.deepStrictEqual(polygonCentroid([[1, 2]]), [NaN, NaN]); + assert.deepStrictEqual(polygonCentroid([[1, 2], [3, 4]]), [NaN, NaN]); +});