@@ -137,11 +137,11 @@ public Polygon Run()
137137 // Process all segments in the clipping polygon
138138 min = new Vertex ( double . PositiveInfinity ) ;
139139 max = new Vertex ( double . NegativeInfinity ) ;
140+
140141 for ( int i = 0 ; i < clipping . ContourCount ; i ++ )
141142 {
142143 Contour contour = clipping [ i ] ;
143- bool exterior = operation != BooleanOperation . Difference ;
144- if ( exterior )
144+ if ( operation != BooleanOperation . Difference )
145145 {
146146 contourId ++ ;
147147 }
@@ -153,8 +153,7 @@ public Polygon Run()
153153 }
154154
155155 Box2 clippingBB = new ( min , max ) ;
156- if ( TryTrivialOperationForNonOverlappingBoundingBoxes ( subject , clipping , subjectBB , clippingBB , operation ,
157- out result ) )
156+ if ( TryTrivialOperationForNonOverlappingBoundingBoxes ( subject , clipping , subjectBB , clippingBB , operation , out result ) )
158157 {
159158 return result ;
160159 }
@@ -393,6 +392,10 @@ private static void ComputeFields(SweepEvent le, SweepEvent? prev, BooleanOperat
393392 {
394393 le . InOut = false ;
395394 le . OtherInOut = true ;
395+
396+ // Clearing the previous result is necessary for recomputing the result.
397+ // if the first computation has set the result it is not valid anymore.
398+ le . PrevInResult = null ;
396399 }
397400 else if ( le . PolygonType == prev . PolygonType )
398401 {
@@ -404,15 +407,28 @@ private static void ComputeFields(SweepEvent le, SweepEvent? prev, BooleanOperat
404407 {
405408 // Previous line segment in sl belongs to a different polygon that "se" belongs to.
406409 le . InOut = ! prev . OtherInOut ;
407- le . OtherInOut = prev . Vertical ( ) ? ! prev . InOut : prev . InOut ;
410+ le . OtherInOut = prev . Vertical ? ! prev . InOut : prev . InOut ;
408411 }
409412
410- // Compute PrevInResult field
413+ // Connect to previous in result: Only use the given `prev` if it is
414+ // part of the result and not a vertical segment. Otherwise connect
415+ // to its previous in result if any.
411416 if ( prev != null )
412417 {
413- le . PrevInResult = ( ! InResult ( prev , operation ) || prev . Vertical ( ) )
414- ? prev . PrevInResult
415- : prev ;
418+ if ( prev . InResult && ! prev . Vertical )
419+ {
420+ le . PrevInResult = prev ;
421+ }
422+ else if ( prev . PrevInResult != null )
423+ {
424+ le . PrevInResult = prev . PrevInResult ;
425+ }
426+ else
427+ {
428+ // Clearing the previous result is necessary for recomputing the result.
429+ // if the first computation has set the result it is not valid anymore.
430+ le . PrevInResult = null ;
431+ }
416432 }
417433
418434 // Check if the line segment belongs to the Boolean operation
@@ -742,11 +758,9 @@ private static Polygon ConnectEdges(List<SweepEvent> sortedEvents, SweepEventCom
742758 for ( int i = 0 ; i < sortedEvents . Count ; i ++ )
743759 {
744760 SweepEvent se = sortedEvents [ i ] ;
745- if ( ( se . Left && se . InResult ) )
746- {
747- resultEvents . Add ( se ) ;
748- }
749- else if ( ! se . Left && se . OtherEvent . InResult )
761+ bool include = ( se . Left && se . InResult ) || ( ! se . Left && se . OtherEvent . InResult ) ;
762+
763+ if ( include )
750764 {
751765 resultEvents . Add ( se ) ;
752766 }
@@ -831,7 +845,6 @@ private static Polygon ConnectEdges(List<SweepEvent> sortedEvents, SweepEventCom
831845
832846 Polygon polygon = new ( ) ;
833847
834-
835848 for ( int i = 0 ; i < result . ContourCount ; i ++ )
836849 {
837850 Contour contour = result [ i ] ;
0 commit comments