Skip to content

Commit bd14671

Browse files
committed
Small ref of SegmentComparer
1 parent aac704b commit bd14671

File tree

2 files changed

+42
-52
lines changed

2 files changed

+42
-52
lines changed

src/PolygonClipper/Helper.cs

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/PolygonClipper/SegmentComparer.cs

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,97 +33,97 @@ public int Compare(SweepEvent? x, SweepEvent? y)
3333
return 1;
3434
}
3535

36-
SweepEvent se_old_l, se_new_l;
37-
Func<bool, int> less_if;
36+
SweepEvent perhapsInversedX, perhapsInversedY;
37+
Func<bool, int> lessIf;
3838

3939
if (x.IsBefore(y))
4040
{
41-
se_old_l = x;
42-
se_new_l = y;
43-
less_if = Helper.LessIf;
41+
perhapsInversedX = x;
42+
perhapsInversedY = y;
43+
lessIf = LessIf;
4444
}
4545
else
4646
{
47-
se_old_l = y;
48-
se_new_l = x;
49-
less_if = Helper.LessIfInversed;
47+
perhapsInversedX = y;
48+
perhapsInversedY = x;
49+
lessIf = LessIfInversed;
5050
}
5151

5252
// 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);
5555

5656
if (area1 != 0 || area2 != 0)
5757
{
5858
// Segments are not collinear
5959
// 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)
6161
{
62-
return less_if(se_old_l.Below(se_new_l.OtherEvent.Point));
62+
return lessIf(perhapsInversedX.Below(perhapsInversedY.OtherEvent.Point));
6363
}
6464

6565
// 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)
6767
{
68-
return less_if(se_old_l.Point.Y < se_new_l.Point.Y);
68+
return lessIf(perhapsInversedX.Point.Y < perhapsInversedY.Point.Y);
6969
}
7070

7171
// If `x` and `y` lie on the same side of the reference segment,
7272
// no intersection check is necessary.
7373
if ((area1 > 0) == (area2 > 0))
7474
{
75-
return less_if(area1 > 0);
75+
return lessIf(area1 > 0);
7676
}
7777

7878
// If `x` lies on the reference segment, compare based on `y`.
7979
if (area1 == 0)
8080
{
81-
return less_if(area2 > 0);
81+
return lessIf(area2 > 0);
8282
}
8383

8484
// 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);
8787

8888
// Call the provided intersection method.
8989
int interResult = PolygonUtilities.FindIntersection(seg0, seg1, out Vertex pi0, out Vertex _);
9090

9191
if (interResult == 0)
9292
{
9393
// No unique intersection found: decide based on area1.
94-
return less_if(area1 > 0);
94+
return lessIf(area1 > 0);
9595
}
9696
else if (interResult == 1)
9797
{
9898
// Unique intersection found.
9999
if (pi0 == y.Point)
100100
{
101-
return less_if(area2 > 0);
101+
return lessIf(area2 > 0);
102102
}
103103

104-
return less_if(area1 > 0);
104+
return lessIf(area1 > 0);
105105
}
106106

107107
// If interResult is neither 0 nor 1, fall through to collinear logic.
108108
}
109109

110110
// Collinear branch – mimicking the Rust logic:
111-
if (se_old_l.PolygonType == se_new_l.PolygonType)
111+
if (perhapsInversedX.PolygonType == perhapsInversedY.PolygonType)
112112
{
113113
// Both segments belong to the same polygon.
114-
if (se_old_l.Point == se_new_l.Point)
114+
if (perhapsInversedX.Point == perhapsInversedY.Point)
115115
{
116116
// 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);
118118
}
119119

120120
// If left endpoints differ, the Rust version simply returns "less" (i.e. the one inserted earlier).
121121
// Here we mimic that by always returning -1.
122-
return less_if(true);
122+
return lessIf(true);
123123
}
124124

125125
// 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);
127127
}
128128

129129
/// <inheritdoc/>
@@ -146,4 +146,20 @@ public int Compare(object? x, object? y)
146146

147147
throw new ArgumentException("Both arguments must be of type SweepEvent.", nameof(x));
148148
}
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;
149165
}

0 commit comments

Comments
 (0)