diff --git a/polylabel.js b/polylabel.js index 2cd4d51..4a4a368 100644 --- a/polylabel.js +++ b/polylabel.js @@ -129,8 +129,9 @@ function getCentroidCell(polygon) { y += (a[1] + b[1]) * f; area += f * 3; } - if (area === 0) return new Cell(points[0][0], points[0][1], 0, polygon); - return new Cell(x / area, y / area, 0, polygon); + const centroid = new Cell(x / area, y / area, 0, polygon); + if (area === 0 || centroid.d < 0) return new Cell(points[0][0], points[0][1], 0, polygon); + return centroid; } // get squared distance from a point to a segment