@@ -408,25 +408,47 @@ private static boolean maybeMergeHoleWithSharedVertices(
408408 // Attempt to find a common point between the HoleNode and OuterNode.
409409 Node sharedVertex = null ;
410410 Node sharedVertexConnection = null ;
411+ // Track the leftmost vertex match (holeNode is the leftmost vertex of the hole)
412+ Node leftmostSharedVertex = null ;
413+ Node leftmostSharedVertexConnection = null ;
411414 Node next = outerNode ;
412415 do {
413416 if (Rectangle .containsPoint (
414417 next .getY (), next .getX (), holeMinY , holeMaxY , holeMinX , holeMaxX )) {
415418 Node newSharedVertex = getSharedVertex (holeNode , next );
416419 if (newSharedVertex != null ) {
420+ // Check if this shared vertex is the leftmost point of the hole (holeNode)
421+ if (isVertexEquals (newSharedVertex , holeNode )) {
422+ if (leftmostSharedVertex == null ) {
423+ leftmostSharedVertex = newSharedVertex ;
424+ leftmostSharedVertexConnection = next ;
425+ } else if (newSharedVertex .equals (leftmostSharedVertex )) {
426+ // Same vertex found again via different connection
427+ leftmostSharedVertexConnection =
428+ getSharedInsideVertex (leftmostSharedVertex , leftmostSharedVertexConnection , next );
429+ }
430+ }
417431 if (sharedVertex == null ) {
418432 sharedVertex = newSharedVertex ;
419433 sharedVertexConnection = next ;
420434 } else if (newSharedVertex .equals (sharedVertex )) {
421- // This can only happen if this vertex has been already used for a bridge. We need to
422- // choose the right one.
435+ // Same vertex found again via different connection.
423436 sharedVertexConnection =
424437 getSharedInsideVertex (sharedVertex , sharedVertexConnection , next );
425438 }
426439 }
427440 }
428441 next = next .next ;
429442 } while (next != outerNode );
443+
444+ // The leftmost vertex of the hole is a shared vertex. Prefer this connection point if it is
445+ // from a hole that was already merged (higher idx), as this maintains proper connectivity
446+ // for chained holes.
447+ if (leftmostSharedVertex != null
448+ && leftmostSharedVertexConnection .idx > sharedVertexConnection .idx ) {
449+ splitPolygon (leftmostSharedVertexConnection , leftmostSharedVertex , true );
450+ return true ;
451+ }
430452 if (sharedVertex != null ) {
431453 // Split the resulting polygon.
432454 splitPolygon (sharedVertexConnection , sharedVertex , true );
0 commit comments