Skip to content

Commit

Permalink
make sure the first guess is not outside the polygon, close #97
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Jun 26, 2024
1 parent cc66e38 commit 416db58
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions polylabel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 416db58

Please sign in to comment.