locationtech / jts

The JTS Topology Suite is a Java library for creating and manipulating vector geometry.
Other
1.94k stars 441 forks source link

Improve ConvexHull radial sort robustness #927

Closed dr-jts closed 1 year ago

dr-jts commented 1 year ago

Improves the robustness of the radial sorting used by the ConvexHull Graham scan algorithm.

This fixes a failure case reported in GEOS: https://github.com/libgeos/geos/issues/722.

The case is

MULTIPOINT (-0.2 -0.1, 1.38777878e-17 -0.1, 0.2 -0.1, -1.38777878e-17 -0.1, -0.2 0.1, 1.38777878e-17 0.1, 0.2 0.1, -1.38777878e-17 0.1)

The case contains points which are extremely close (below the precision of double-precision floating point). This caused the old code to fail, since it was unable to robustly compute a different distance for the close points, and thus sorted the points incorrectly. This in turn caused an incorrect result from the Graham Scan algorithm: image

The fix is to use a more robust way of computing relative distance for the radial sort.

Signed-off-by: Martin Davis mtnclimb@gmail.com