i_overlay 6.0.0

Boolean Operations for 2D Polygons: Supports intersection, union, difference, xor, and self-intersections for all polygon varieties.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
use crate::build::sweep::FillHandler;
use crate::segm::boolean::ShapeCountBoolean;
use crate::segm::segment::{
    BOTH_BOTTOM, BOTH_TOP, CLIP_BOTH, CLIP_BOTTOM, CLIP_TOP, SUBJ_BOTH, SUBJ_BOTTOM, SUBJ_TOP, Segment,
    SegmentFill,
};
use alloc::vec::Vec;
use core::ops::ControlFlow;
use i_float::int::point::IntPoint;
use i_key_sort::sort::two_keys::TwoKeysSort;

/// Collects segment endpoints and checks for coincidence between subject and clip.
///
/// Uses optimized algorithm: collect into separate Vecs, sort with `sort_by_two_keys`,
/// dedup, then binary search from shorter into longer array.
pub(crate) struct PointCoincidenceChecker {
    subj_points: Vec<IntPoint>,
    clip_points: Vec<IntPoint>,
}

impl PointCoincidenceChecker {
    /// Create a new checker with pre-allocated capacity.
    ///
    /// `capacity` is the number of segments; each segment contributes 2 endpoints.
    #[inline]
    pub(crate) fn new(capacity: usize) -> Self {
        Self {
            subj_points: Vec::with_capacity(capacity * 2),
            clip_points: Vec::with_capacity(capacity * 2),
        }
    }

    /// Add a segment's endpoints based on its count and fill.
    ///
    /// Uses fill to skip inner segments that can't contribute to boundary coincidence:
    /// - Segments entirely inside subject (SUBJ_BOTH, no clip contribution) with no
    ///   clip in the segment are skipped for clip collection
    /// - Similarly for clip-only interior segments
    #[inline]
    pub(crate) fn add_segment(&mut self, segment: &Segment<ShapeCountBoolean>, fill: SegmentFill) {
        // Skip inner segments optimization:
        // If segment is entirely inside one shape's interior (filled on both sides)
        // and has no contribution from the other shape, it's not on a boundary
        // where coincidence could occur.
        let subj_interior = (fill & SUBJ_BOTH) == SUBJ_BOTH;
        let clip_interior = (fill & CLIP_BOTH) == CLIP_BOTH;

        if subj_interior || clip_interior || fill == 0 {
            return;
        }

        let is_subj = fill & SUBJ_BOTH != 0;
        let is_clip = fill & CLIP_BOTH != 0;
        if is_subj && is_clip {
            // Segment belongs to both shapes (boundary contact) - this is a shared edge, not a point coincidence.
            return;
        }
        if is_subj {
            self.subj_points.push(segment.x_segment.a);
            self.subj_points.push(segment.x_segment.b);
        } else {
            debug_assert!(is_clip);
            self.clip_points.push(segment.x_segment.a);
            self.clip_points.push(segment.x_segment.b);
        }
    }

    /// Check if any subject point coincides with any clip point.
    ///
    /// Consumes self and returns true if coincidence found.
    ///
    /// Optimization: Only sort/dedup the shorter array, then iterate the longer
    /// array doing binary searches into the shorter. This minimizes total work:
    /// O(n log n) sort + O(m log n) searches, where n ≤ m.
    #[inline]
    pub(crate) fn has_coincidence(mut self) -> bool {
        if self.subj_points.is_empty() || self.clip_points.is_empty() {
            return false;
        }

        // Determine shorter/longer by pre-dedup size (good estimate of post-dedup)
        let (shorter, longer) = if self.subj_points.len() <= self.clip_points.len() {
            (&mut self.subj_points, &self.clip_points)
        } else {
            (&mut self.clip_points, &self.subj_points)
        };

        // Sort and dedup only the shorter array (binary search target)
        shorter.sort_by_two_keys(false, |p| p.x, |p| p.y);
        shorter.dedup();

        // Iterate longer (unsorted) and binary search into shorter
        longer.iter().any(|p| shorter.binary_search(p).is_ok())
    }
}

/// Handler that checks if subject and clip shapes intersect (share any point).
///
/// Returns `true` on the first segment where both shapes contribute fill,
/// indicating the geometries share at least one point (interior overlap or boundary contact).
/// This matches the DE-9IM definition of `intersects`.
///
/// This handler is designed for early-exit optimization - it breaks out of the sweep
/// loop as soon as an intersection is detected, avoiding processing of remaining segments.
///
/// Also collects endpoint information for point coincidence check in finalize.
pub(crate) struct IntersectsHandler {
    point_checker: PointCoincidenceChecker,
}

impl IntersectsHandler {
    pub(crate) fn new(capacity: usize) -> Self {
        Self {
            point_checker: PointCoincidenceChecker::new(capacity),
        }
    }
}

impl FillHandler<ShapeCountBoolean> for IntersectsHandler {
    type Output = bool;

    #[inline(always)]
    fn handle(
        &mut self,
        _index: usize,
        segment: &Segment<ShapeCountBoolean>,
        fill: SegmentFill,
    ) -> ControlFlow<bool> {
        // Shapes intersect if both contribute to any segment (interior overlap or boundary contact)
        let has_subj = (fill & SUBJ_BOTH) != 0;
        let has_clip = (fill & CLIP_BOTH) != 0;
        if has_subj && has_clip {
            ControlFlow::Break(true)
        } else {
            self.point_checker.add_segment(segment, fill);
            ControlFlow::Continue(())
        }
    }

    #[inline(always)]
    fn finalize(self) -> bool {
        self.point_checker.has_coincidence()
    }
}

/// Handler that checks if the interiors of subject and clip shapes overlap.
///
/// Returns `true` when both shapes have fill on the same side of a segment,
/// indicating their interiors share area. This is stricter than `intersects`
/// which also returns true for boundary-only contact.
///
/// Early-exits `true` on first interior overlap.
pub(crate) struct InteriorsIntersectHandler;

impl FillHandler<ShapeCountBoolean> for InteriorsIntersectHandler {
    type Output = bool;

    #[inline(always)]
    fn handle(
        &mut self,
        _index: usize,
        _segment: &Segment<ShapeCountBoolean>,
        fill: SegmentFill,
    ) -> ControlFlow<bool> {
        // Interiors intersect if both shapes fill the same side
        if (fill & BOTH_TOP) == BOTH_TOP || (fill & BOTH_BOTTOM) == BOTH_BOTTOM {
            ControlFlow::Break(true)
        } else {
            ControlFlow::Continue(())
        }
    }

    #[inline(always)]
    fn finalize(self) -> bool {
        false
    }
}

/// Handler that checks if subject and clip shapes touch (boundaries intersect but interiors don't).
///
/// Returns `true` if boundaries contact without interior overlap.
/// Early-exits with `false` on first interior overlap since that definitively means
/// the shapes don't just touch.
///
/// Also collects endpoint information for point coincidence check in finalize.
pub(crate) struct TouchesHandler {
    has_boundary_contact: bool,
    point_checker: PointCoincidenceChecker,
}

impl TouchesHandler {
    pub(crate) fn new(capacity: usize) -> Self {
        Self {
            has_boundary_contact: false,
            point_checker: PointCoincidenceChecker::new(capacity),
        }
    }
}

impl FillHandler<ShapeCountBoolean> for TouchesHandler {
    type Output = bool;

    #[inline(always)]
    fn handle(
        &mut self,
        _index: usize,
        segment: &Segment<ShapeCountBoolean>,
        fill: SegmentFill,
    ) -> ControlFlow<bool> {
        // Interior overlap = not a touch (early exit false)
        if (fill & BOTH_TOP) == BOTH_TOP || (fill & BOTH_BOTTOM) == BOTH_BOTTOM {
            return ControlFlow::Break(false);
        }
        // Track boundary contact
        if (fill & SUBJ_BOTH) != 0 && (fill & CLIP_BOTH) != 0 {
            self.has_boundary_contact = true;
        }
        self.point_checker.add_segment(segment, fill);
        ControlFlow::Continue(())
    }

    #[inline(always)]
    fn finalize(self) -> bool {
        self.has_boundary_contact || self.point_checker.has_coincidence()
    }
}

/// Handler that checks if subject and clip shapes intersect by point coincidence only.
///
/// Returns `true` if shapes share boundary vertices but NOT edges.
/// - Returns `false` if there's interior overlap (early exit)
/// - Returns `false` if there's edge/boundary contact (shared segments, early exit)
/// - Returns `true` ONLY if shapes touch by point coincidence without any edge overlap
pub(crate) struct PointIntersectsHandler {
    point_checker: PointCoincidenceChecker,
}

impl PointIntersectsHandler {
    pub(crate) fn new(capacity: usize) -> Self {
        Self {
            point_checker: PointCoincidenceChecker::new(capacity),
        }
    }
}

impl FillHandler<ShapeCountBoolean> for PointIntersectsHandler {
    type Output = bool;

    #[inline(always)]
    fn handle(
        &mut self,
        _index: usize,
        segment: &Segment<ShapeCountBoolean>,
        fill: SegmentFill,
    ) -> ControlFlow<bool> {
        // Interior overlap = not a point-only intersection (early exit false)
        if (fill & BOTH_TOP) == BOTH_TOP || (fill & BOTH_BOTTOM) == BOTH_BOTTOM {
            return ControlFlow::Break(false);
        }
        // Boundary contact (edge sharing) = not point-only (early exit false)
        if (fill & SUBJ_BOTH) != 0 && (fill & CLIP_BOTH) != 0 {
            return ControlFlow::Break(false);
        }
        self.point_checker.add_segment(segment, fill);
        ControlFlow::Continue(())
    }

    #[inline(always)]
    fn finalize(self) -> bool {
        self.point_checker.has_coincidence()
    }
}

/// Handler that checks if subject is completely within clip.
///
/// Returns `true` if everywhere the subject has fill, the clip also has fill
/// on the same side. Early-exits `false` on first violation.
pub(crate) struct WithinHandler {
    subj_present: bool,
}

impl WithinHandler {
    pub(crate) fn new() -> Self {
        Self { subj_present: false }
    }
}

impl FillHandler<ShapeCountBoolean> for WithinHandler {
    type Output = bool;

    #[inline(always)]
    fn handle(
        &mut self,
        _index: usize,
        _segment: &Segment<ShapeCountBoolean>,
        fill: SegmentFill,
    ) -> ControlFlow<bool> {
        let subj_top = (fill & SUBJ_TOP) != 0;
        let subj_bot = (fill & SUBJ_BOTTOM) != 0;
        let clip_top = (fill & CLIP_TOP) != 0;
        let clip_bot = (fill & CLIP_BOTTOM) != 0;

        if subj_top || subj_bot {
            self.subj_present = true;
        }

        // Subject filled where clip isn't = not within
        if (subj_top && !clip_top) || (subj_bot && !clip_bot) {
            ControlFlow::Break(false)
        } else {
            ControlFlow::Continue(())
        }
    }

    #[inline(always)]
    fn finalize(self) -> bool {
        // Empty subject is not within anything
        self.subj_present
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::geom::x_segment::XSegment;

    fn make_segment(ax: i32, ay: i32, bx: i32, by: i32, subj: i32, clip: i32) -> Segment<ShapeCountBoolean> {
        Segment {
            x_segment: XSegment {
                a: IntPoint::new(ax, ay),
                b: IntPoint::new(bx, by),
            },
            count: ShapeCountBoolean { subj, clip },
        }
    }

    #[test]
    fn test_point_coincidence_no_points() {
        let checker = PointCoincidenceChecker::new(10);
        assert!(!checker.has_coincidence());
    }

    #[test]
    fn test_point_coincidence_subj_only() {
        let mut checker = PointCoincidenceChecker::new(10);
        checker.add_segment(&make_segment(0, 0, 10, 0, 1, 0), SUBJ_TOP);
        assert!(!checker.has_coincidence());
    }

    #[test]
    fn test_point_coincidence_coincident_point() {
        let mut checker = PointCoincidenceChecker::new(10);
        // Subject segment with endpoint at (10, 10)
        checker.add_segment(&make_segment(0, 0, 10, 10, 1, 0), SUBJ_TOP);
        // Clip segment with endpoint at (10, 10)
        checker.add_segment(&make_segment(10, 10, 20, 20, 0, 1), CLIP_TOP);
        assert!(checker.has_coincidence());
    }

    #[test]
    fn test_point_coincidence_no_coincidence() {
        let mut checker = PointCoincidenceChecker::new(10);
        checker.add_segment(&make_segment(0, 0, 5, 5, 1, 0), SUBJ_TOP);
        checker.add_segment(&make_segment(10, 10, 20, 20, 0, 1), CLIP_TOP);
        assert!(!checker.has_coincidence());
    }

    #[test]
    fn test_point_coincidence_shared_segment_is_line_not_point() {
        let mut checker = PointCoincidenceChecker::new(10);
        // Segment with both SUBJ and CLIP fill is a shared edge (line intersection),
        // not a point coincidence. Only one array gets populated, so no coincidence.
        checker.add_segment(&make_segment(0, 0, 10, 10, 1, 1), SUBJ_TOP | CLIP_BOTTOM);
        assert!(!checker.has_coincidence());
    }

    #[test]
    fn test_point_coincidence_dedup_works() {
        let mut checker = PointCoincidenceChecker::new(10);
        // Two subject segments sharing endpoint (5, 5)
        checker.add_segment(&make_segment(0, 0, 5, 5, 1, 0), SUBJ_TOP);
        checker.add_segment(&make_segment(5, 5, 10, 10, 1, 0), SUBJ_TOP);
        // Clip at (5, 5)
        checker.add_segment(&make_segment(5, 5, 15, 15, 0, 1), CLIP_TOP);
        assert!(checker.has_coincidence());
    }

    #[test]
    fn test_intersects_handler_both_top() {
        let seg = make_segment(0, 0, 10, 0, 1, 1);
        let mut handler = IntersectsHandler::new(10);
        let fill = SUBJ_TOP | CLIP_TOP;
        let result = handler.handle(0, &seg, fill);
        assert!(matches!(result, ControlFlow::Break(true)));
    }

    #[test]
    fn test_intersects_handler_both_bottom() {
        let seg = make_segment(0, 0, 10, 0, 1, 1);
        let mut handler = IntersectsHandler::new(10);
        let fill = SUBJ_BOTTOM | CLIP_BOTTOM;
        let result = handler.handle(0, &seg, fill);
        assert!(matches!(result, ControlFlow::Break(true)));
    }

    #[test]
    fn test_intersects_handler_boundary_contact() {
        // Boundary contact (edge sharing) is still an intersection per DE-9IM
        let seg = make_segment(0, 0, 10, 0, 1, 1);
        let mut handler = IntersectsHandler::new(10);
        let fill = SUBJ_TOP | CLIP_BOTTOM;
        let result = handler.handle(0, &seg, fill);
        assert!(matches!(result, ControlFlow::Break(true)));
    }

    #[test]
    fn test_intersects_handler_no_intersection() {
        // Only subject contributes - no intersection
        let seg = make_segment(0, 0, 10, 0, 1, 0);
        let mut handler = IntersectsHandler::new(10);
        let fill = SUBJ_TOP;
        let result = handler.handle(0, &seg, fill);
        assert!(matches!(result, ControlFlow::Continue(())));

        // Only clip contributes - no intersection
        let seg = make_segment(0, 0, 10, 0, 0, 1);
        let fill = CLIP_BOTTOM;
        let result = handler.handle(0, &seg, fill);
        assert!(matches!(result, ControlFlow::Continue(())));
    }

    #[test]
    fn test_intersects_handler_finalize_with_coincidence() {
        let mut handler = IntersectsHandler::new(10);
        // Add segments that don't trigger early exit but have point coincidence
        let seg1 = make_segment(0, 0, 10, 10, 1, 0);
        let seg2 = make_segment(10, 10, 20, 20, 0, 1);
        let _ = handler.handle(0, &seg1, SUBJ_TOP);
        let _ = handler.handle(1, &seg2, CLIP_TOP);
        assert!(handler.finalize());
    }

    #[test]
    fn test_interiors_intersect_handler_both_top() {
        let seg = make_segment(0, 0, 10, 0, 1, 1);
        let mut handler = InteriorsIntersectHandler;
        let fill = SUBJ_TOP | CLIP_TOP;
        let result = handler.handle(0, &seg, fill);
        assert!(matches!(result, ControlFlow::Break(true)));
    }

    #[test]
    fn test_interiors_intersect_handler_both_bottom() {
        let seg = make_segment(0, 0, 10, 0, 1, 1);
        let mut handler = InteriorsIntersectHandler;
        let fill = SUBJ_BOTTOM | CLIP_BOTTOM;
        let result = handler.handle(0, &seg, fill);
        assert!(matches!(result, ControlFlow::Break(true)));
    }

    #[test]
    fn test_interiors_intersect_handler_boundary_only() {
        // Boundary contact without interior overlap
        let seg = make_segment(0, 0, 10, 0, 1, 1);
        let mut handler = InteriorsIntersectHandler;
        let fill = SUBJ_TOP | CLIP_BOTTOM;
        let result = handler.handle(0, &seg, fill);
        assert!(matches!(result, ControlFlow::Continue(())));
        assert!(!handler.finalize());
    }

    #[test]
    fn test_touches_handler_boundary_only() {
        let seg = make_segment(0, 0, 10, 0, 1, 1);
        let mut handler = TouchesHandler::new(10);
        let fill = SUBJ_TOP | CLIP_BOTTOM;
        let result = handler.handle(0, &seg, fill);
        assert!(matches!(result, ControlFlow::Continue(())));
        assert!(handler.finalize()); // boundary contact, no interior overlap
    }

    #[test]
    fn test_touches_handler_interior_overlap() {
        let seg = make_segment(0, 0, 10, 0, 1, 1);
        let mut handler = TouchesHandler::new(10);
        let fill = SUBJ_TOP | CLIP_TOP;
        let result = handler.handle(0, &seg, fill);
        assert!(matches!(result, ControlFlow::Break(false))); // early exit on interior overlap
    }

    #[test]
    fn test_touches_handler_no_contact() {
        let seg = make_segment(0, 0, 10, 0, 1, 0);
        let mut handler = TouchesHandler::new(10);
        let fill = SUBJ_TOP;
        let result = handler.handle(0, &seg, fill);
        assert!(matches!(result, ControlFlow::Continue(())));
        assert!(!handler.finalize()); // no boundary contact, no interior overlap
    }

    #[test]
    fn test_touches_handler_point_coincidence() {
        let mut handler = TouchesHandler::new(10);
        // Add segments that don't touch via fill but have point coincidence
        let seg1 = make_segment(0, 0, 10, 10, 1, 0);
        let seg2 = make_segment(10, 10, 20, 20, 0, 1);
        let _ = handler.handle(0, &seg1, SUBJ_TOP);
        let _ = handler.handle(1, &seg2, CLIP_TOP);
        assert!(handler.finalize());
    }

    #[test]
    fn test_within_handler_subject_inside_clip() {
        let seg = make_segment(0, 0, 10, 0, 1, 1);
        let mut handler = WithinHandler::new();
        // Subject has top fill, clip also has top fill - subject is within
        let fill = SUBJ_TOP | CLIP_TOP;
        let result = handler.handle(0, &seg, fill);
        assert!(matches!(result, ControlFlow::Continue(())));
        assert!(handler.finalize());
    }

    #[test]
    fn test_within_handler_subject_outside_clip() {
        let seg = make_segment(0, 0, 10, 0, 1, 0);
        let mut handler = WithinHandler::new();
        // Subject has top fill but clip doesn't - subject is outside
        let fill = SUBJ_TOP;
        let result = handler.handle(0, &seg, fill);
        assert!(matches!(result, ControlFlow::Break(false)));
    }

    #[test]
    fn test_within_handler_empty_subject() {
        let handler = WithinHandler::new();
        // Empty subject is not within anything
        assert!(!handler.finalize());
    }

    #[test]
    fn test_within_handler_clip_only() {
        let seg = make_segment(0, 0, 10, 0, 0, 1);
        let mut handler = WithinHandler::new();
        // Only clip contributes - ok, but need subject present
        let fill = CLIP_TOP;
        let result = handler.handle(0, &seg, fill);
        assert!(matches!(result, ControlFlow::Continue(())));
        assert!(!handler.finalize());
    }

    #[test]
    fn test_point_intersects_handler_point_only() {
        let mut handler = PointIntersectsHandler::new(10);
        // Subject segment ending at (10, 10)
        let seg1 = make_segment(0, 0, 10, 10, 1, 0);
        // Clip segment starting at (10, 10)
        let seg2 = make_segment(10, 10, 20, 20, 0, 1);
        let _ = handler.handle(0, &seg1, SUBJ_TOP);
        let _ = handler.handle(1, &seg2, CLIP_TOP);
        // Point coincidence without edge contact → true
        assert!(handler.finalize());
    }

    #[test]
    fn test_point_intersects_handler_edge_contact() {
        // Segment belongs to both subject and clip (shared edge)
        let seg = make_segment(0, 0, 10, 0, 1, 1);
        let mut handler = PointIntersectsHandler::new(10);
        // Both shapes have fill on opposite sides (boundary contact)
        let fill = SUBJ_TOP | CLIP_BOTTOM;
        let result = handler.handle(0, &seg, fill);
        // Early exit false on boundary contact (edge sharing)
        assert!(matches!(result, ControlFlow::Break(false)));
    }

    #[test]
    fn test_point_intersects_handler_interior_overlap() {
        let seg = make_segment(0, 0, 10, 0, 1, 1);
        let mut handler = PointIntersectsHandler::new(10);
        // Interior overlap (both shapes fill the same side)
        let fill = SUBJ_TOP | CLIP_TOP;
        let result = handler.handle(0, &seg, fill);
        // Early exit false on interior overlap
        assert!(matches!(result, ControlFlow::Break(false)));
    }

    #[test]
    fn test_point_intersects_handler_no_contact() {
        let seg1 = make_segment(0, 0, 5, 5, 1, 0);
        let seg2 = make_segment(10, 10, 20, 20, 0, 1);
        let mut handler = PointIntersectsHandler::new(10);
        let _ = handler.handle(0, &seg1, SUBJ_TOP);
        let _ = handler.handle(1, &seg2, CLIP_TOP);
        // No contact at all → false
        assert!(!handler.finalize());
    }
}