Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions src/area.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
35 changes: 20 additions & 15 deletions src/centroid.js
Original file line number Diff line number Diff line change
@@ -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];
}
15 changes: 15 additions & 0 deletions test/area-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
15 changes: 15 additions & 0 deletions test/centroid-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});