@@ -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-
141140 for ( int i = 0 ; i < clipping . ContourCount ; i ++ )
142141 {
143142 Contour contour = clipping [ i ] ;
144- if ( operation != BooleanOperation . Difference )
143+ bool exterior = operation != BooleanOperation . Difference ;
144+ if ( exterior )
145145 {
146146 contourId ++ ;
147147 }
@@ -153,7 +153,8 @@ public Polygon Run()
153153 }
154154
155155 Box2 clippingBB = new ( min , max ) ;
156- if ( TryTrivialOperationForNonOverlappingBoundingBoxes ( subject , clipping , subjectBB , clippingBB , operation , out result ) )
156+ if ( TryTrivialOperationForNonOverlappingBoundingBoxes ( subject , clipping , subjectBB , clippingBB , operation ,
157+ out result ) )
157158 {
158159 return result ;
159160 }
@@ -392,10 +393,6 @@ private static void ComputeFields(SweepEvent le, SweepEvent? prev, BooleanOperat
392393 {
393394 le . InOut = false ;
394395 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 ;
399396 }
400397 else if ( le . PolygonType == prev . PolygonType )
401398 {
@@ -407,28 +404,15 @@ private static void ComputeFields(SweepEvent le, SweepEvent? prev, BooleanOperat
407404 {
408405 // Previous line segment in sl belongs to a different polygon that "se" belongs to.
409406 le . InOut = ! prev . OtherInOut ;
410- le . OtherInOut = prev . Vertical ? ! prev . InOut : prev . InOut ;
407+ le . OtherInOut = prev . Vertical ( ) ? ! prev . InOut : prev . InOut ;
411408 }
412409
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.
410+ // Compute PrevInResult field
416411 if ( prev != null )
417412 {
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- }
413+ le . PrevInResult = ( ! InResult ( prev , operation ) || prev . Vertical ( ) )
414+ ? prev . PrevInResult
415+ : prev ;
432416 }
433417
434418 // Check if the line segment belongs to the Boolean operation
@@ -758,9 +742,11 @@ private static Polygon ConnectEdges(List<SweepEvent> sortedEvents, SweepEventCom
758742 for ( int i = 0 ; i < sortedEvents . Count ; i ++ )
759743 {
760744 SweepEvent se = sortedEvents [ i ] ;
761- bool include = ( se . Left && se . InResult ) || ( ! se . Left && se . OtherEvent . InResult ) ;
762-
763- if ( include )
745+ if ( ( se . Left && se . InResult ) )
746+ {
747+ resultEvents . Add ( se ) ;
748+ }
749+ else if ( ! se . Left && se . OtherEvent . InResult )
764750 {
765751 resultEvents . Add ( se ) ;
766752 }
@@ -845,6 +831,7 @@ private static Polygon ConnectEdges(List<SweepEvent> sortedEvents, SweepEventCom
845831
846832 Polygon polygon = new ( ) ;
847833
834+
848835 for ( int i = 0 ; i < result . ContourCount ; i ++ )
849836 {
850837 Contour contour = result [ i ] ;
0 commit comments