Skip to content

Commit 3cdb9f6

Browse files
authored
Release/0.22.5 (#74)
Fixing error in counting vertex numbers
1 parent 93cb13f commit 3cdb9f6

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/geometry/java/us/ihmc/euclid/geometry/tools/EuclidGeometryPolygonTools.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,23 @@ public static int inPlaceGiftWrapConvexHull2D(List<? extends Point2DReadOnly> ve
236236
return numberOfVertices;
237237
}
238238

239-
while ((lastHullVertexIndex >= 1 && isCandidatePointCollinearInOppositeDirectionWithTheFirstPoint(lastHullVertex,
240-
vertices.get(lastHullVertexIndex - 1),
241-
candidateVertex,
242-
EPSILON)))
239+
while (candidateIndex < numberOfVertices && lastHullVertexIndex >= 1
240+
&& isCandidatePointCollinearInOppositeDirectionWithTheFirstPoint(lastHullVertex,
241+
vertices.get(lastHullVertexIndex- 1),
242+
candidateVertex,
243+
EPSILON))
243244
{ // The next candidate vertex isn't valid because it's collinear, but may be valid for a future point, so we want to keep it in scope.
244245
candidateIndex++;
246+
247+
// If we have skipped over all remaining vertices (e.g., due to duplicates or collinear points),
248+
// there is no valid candidate left to add to the convex hull.
249+
// In this case, we terminate the wrapping early and return the number of hull vertices found so far.
250+
// Since lastHullVertexIndex is the index of the last added vertex, the total count is lastHullVertexIndex + 1.
251+
if (candidateIndex >= numberOfVertices)
252+
{
253+
return lastHullVertexIndex + 1;
254+
}
255+
245256
candidateVertex = vertices.get(candidateIndex);
246257
}
247258

0 commit comments

Comments
 (0)