@@ -33,97 +33,97 @@ public int Compare(SweepEvent? x, SweepEvent? y)
33
33
return 1 ;
34
34
}
35
35
36
- SweepEvent se_old_l , se_new_l ;
37
- Func < bool , int > less_if ;
36
+ SweepEvent perhapsInversedX , perhapsInversedY ;
37
+ Func < bool , int > lessIf ;
38
38
39
39
if ( x . IsBefore ( y ) )
40
40
{
41
- se_old_l = x ;
42
- se_new_l = y ;
43
- less_if = Helper . LessIf ;
41
+ perhapsInversedX = x ;
42
+ perhapsInversedY = y ;
43
+ lessIf = LessIf ;
44
44
}
45
45
else
46
46
{
47
- se_old_l = y ;
48
- se_new_l = x ;
49
- less_if = Helper . LessIfInversed ;
47
+ perhapsInversedX = y ;
48
+ perhapsInversedY = x ;
49
+ lessIf = LessIfInversed ;
50
50
}
51
51
52
52
// Check if the segments are collinear by comparing their signed areas
53
- double area1 = PolygonUtilities . SignedArea ( se_old_l . Point , se_old_l . OtherEvent . Point , se_new_l . Point ) ;
54
- double area2 = PolygonUtilities . SignedArea ( se_old_l . Point , se_old_l . OtherEvent . Point , se_new_l . OtherEvent . Point ) ;
53
+ double area1 = PolygonUtilities . SignedArea ( perhapsInversedX . Point , perhapsInversedX . OtherEvent . Point , perhapsInversedY . Point ) ;
54
+ double area2 = PolygonUtilities . SignedArea ( perhapsInversedX . Point , perhapsInversedX . OtherEvent . Point , perhapsInversedY . OtherEvent . Point ) ;
55
55
56
56
if ( area1 != 0 || area2 != 0 )
57
57
{
58
58
// Segments are not collinear
59
59
// If they share their left endpoint, use the right endpoint to sort
60
- if ( se_old_l . Point == se_new_l . Point )
60
+ if ( perhapsInversedX . Point == perhapsInversedY . Point )
61
61
{
62
- return less_if ( se_old_l . Below ( se_new_l . OtherEvent . Point ) ) ;
62
+ return lessIf ( perhapsInversedX . Below ( perhapsInversedY . OtherEvent . Point ) ) ;
63
63
}
64
64
65
65
// Different left endpoints: use the y-coordinate to sort if x-coordinates are the same
66
- if ( se_old_l . Point . X == se_new_l . Point . X )
66
+ if ( perhapsInversedX . Point . X == perhapsInversedY . Point . X )
67
67
{
68
- return less_if ( se_old_l . Point . Y < se_new_l . Point . Y ) ;
68
+ return lessIf ( perhapsInversedX . Point . Y < perhapsInversedY . Point . Y ) ;
69
69
}
70
70
71
71
// If `x` and `y` lie on the same side of the reference segment,
72
72
// no intersection check is necessary.
73
73
if ( ( area1 > 0 ) == ( area2 > 0 ) )
74
74
{
75
- return less_if ( area1 > 0 ) ;
75
+ return lessIf ( area1 > 0 ) ;
76
76
}
77
77
78
78
// If `x` lies on the reference segment, compare based on `y`.
79
79
if ( area1 == 0 )
80
80
{
81
- return less_if ( area2 > 0 ) ;
81
+ return lessIf ( area2 > 0 ) ;
82
82
}
83
83
84
84
// Form segments from the events.
85
- Segment seg0 = new ( se_old_l . Point , se_old_l . OtherEvent . Point ) ;
86
- Segment seg1 = new ( se_new_l . Point , se_new_l . OtherEvent . Point ) ;
85
+ Segment seg0 = new ( perhapsInversedX . Point , perhapsInversedX . OtherEvent . Point ) ;
86
+ Segment seg1 = new ( perhapsInversedY . Point , perhapsInversedY . OtherEvent . Point ) ;
87
87
88
88
// Call the provided intersection method.
89
89
int interResult = PolygonUtilities . FindIntersection ( seg0 , seg1 , out Vertex pi0 , out Vertex _ ) ;
90
90
91
91
if ( interResult == 0 )
92
92
{
93
93
// No unique intersection found: decide based on area1.
94
- return less_if ( area1 > 0 ) ;
94
+ return lessIf ( area1 > 0 ) ;
95
95
}
96
96
else if ( interResult == 1 )
97
97
{
98
98
// Unique intersection found.
99
99
if ( pi0 == y . Point )
100
100
{
101
- return less_if ( area2 > 0 ) ;
101
+ return lessIf ( area2 > 0 ) ;
102
102
}
103
103
104
- return less_if ( area1 > 0 ) ;
104
+ return lessIf ( area1 > 0 ) ;
105
105
}
106
106
107
107
// If interResult is neither 0 nor 1, fall through to collinear logic.
108
108
}
109
109
110
110
// Collinear branch – mimicking the Rust logic:
111
- if ( se_old_l . PolygonType == se_new_l . PolygonType )
111
+ if ( perhapsInversedX . PolygonType == perhapsInversedY . PolygonType )
112
112
{
113
113
// Both segments belong to the same polygon.
114
- if ( se_old_l . Point == se_new_l . Point )
114
+ if ( perhapsInversedX . Point == perhapsInversedY . Point )
115
115
{
116
116
// When left endpoints are identical, order by contour id.
117
- return less_if ( se_old_l . ContourId < se_new_l . ContourId ) ;
117
+ return lessIf ( perhapsInversedX . ContourId < perhapsInversedY . ContourId ) ;
118
118
}
119
119
120
120
// If left endpoints differ, the Rust version simply returns "less" (i.e. the one inserted earlier).
121
121
// Here we mimic that by always returning -1.
122
- return less_if ( true ) ;
122
+ return lessIf ( true ) ;
123
123
}
124
124
125
125
// Segments are collinear but belong to different polygons.
126
- return less_if ( se_old_l . PolygonType == PolygonType . Subject ) ;
126
+ return lessIf ( perhapsInversedX . PolygonType == PolygonType . Subject ) ;
127
127
}
128
128
129
129
/// <inheritdoc/>
@@ -146,4 +146,20 @@ public int Compare(object? x, object? y)
146
146
147
147
throw new ArgumentException ( "Both arguments must be of type SweepEvent." , nameof ( x ) ) ;
148
148
}
149
+
150
+ /// <summary>
151
+ /// Converts a boolean comparison result to an ordering value.
152
+ /// Returns -1 if the condition is true, 1 if false.
153
+ /// </summary>
154
+ /// <param name="condition">The boolean condition to evaluate.</param>
155
+ /// <returns>-1 if condition is true, 1 if false.</returns>
156
+ public static int LessIf ( bool condition ) => condition ? - 1 : 1 ;
157
+
158
+ /// <summary>
159
+ /// Converts a boolean comparison result to an inversed ordering value.
160
+ /// Returns 1 if the condition is true, -1 if false.
161
+ /// </summary>
162
+ /// <param name="condition">The boolean condition to evaluate.</param>
163
+ /// <returns>1 if condition is true, -1 if false.</returns>
164
+ public static int LessIfInversed ( bool condition ) => condition ? 1 : - 1 ;
149
165
}
0 commit comments