planar_geo 0.3.4

A Rust library for 2D geometry: geometric objects, algorithms and visualization
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
/*!
This module contains the [`Primitive`] trait and the [`PrimitiveIntersections`]
enum.

The geometric types in this crate are either "primitives" (which implement the
[`Primitive`] trait) or "composites" (which are made up from multiple primitives
and implement the [`Composite`](crate::composite::Composite) trait). Primitives
are simple types such as points (`[f64; 2]`), [`Line`]s,
[`LineSegment`]s or [`ArcSegment`]s. The [`Primitive`] trait provides a common
interface for intersection calculation between primitives which always return
a [`PrimitiveIntersections`]. The intersection algorithms between the composite
types are then based upon these.
 */

use crate::{
    line::Line,
    segment::{ArcSegment, LineSegment, Segment},
};
use approx::UlpsEq;

/**
Result of an intersection calculation between two [`Primitive`]s.

This enum is usually created by one of the intersection functions of the
[`Primitive`] trait. It offers the following trait implementations:
- The [`std::ops::Index`] trait for treating this enum as an
array of length 0, 1 or 2 (depending on the variant).
- The [`IntoIterator`] trait to iterate over all intersection points `[f64; 2]`.
- The [`PartialEq`] trait to compare intersections. The order
of elements in the [`PrimitiveIntersections::Two`] variant does not matter:
```
use planar_geo::primitive::PrimitiveIntersections;

assert_eq!(
    PrimitiveIntersections::Two([[0.0, 0.0], [1.0, 1.0]]),
    PrimitiveIntersections::Two([[1.0, 1.0], [0.0, 0.0]])
)
```
- The [`AbsDiffEq`](approx::AbsDiffEq), [`RelativeEq`](approx::RelativeEq) and
[`UlpsEq`] traits from the [approx] crate to allow for approximate equality
comparison. This should generally be preferred over comparing for exact equality
via the [`PartialEq`] trait when dealing with floating point values.
 */
#[derive(Clone, Copy, Debug)]
pub enum PrimitiveIntersections {
    /// The primitives don't intersect at all.
    Zero,
    /// The primitives have a single intersection point.
    One([f64; 2]),
    /// The primitives intersect in two points.
    Two([[f64; 2]; 2]),
}

impl PartialEq for PrimitiveIntersections {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::One(l), Self::One(r)) => l == r,
            (Self::Two(l), Self::Two(r)) => {
                (l[0] == r[0] && l[1] == r[1]) || (l[0] == r[1] && l[1] == r[0])
            }
            _ => core::mem::discriminant(self) == core::mem::discriminant(other),
        }
    }
}

impl IntoIterator for PrimitiveIntersections {
    type Item = [f64; 2];
    type IntoIter = PrimitiveIntersectionsIntoIter;

    fn into_iter(self) -> Self::IntoIter {
        let intersections = match self {
            PrimitiveIntersections::Zero => [None, None],
            PrimitiveIntersections::One(i) => [Some(i), None],
            PrimitiveIntersections::Two(ai) => [Some(ai[0]), Some(ai[1])],
        };
        return PrimitiveIntersectionsIntoIter {
            intersections,
            counter: 0,
        };
    }
}

impl approx::AbsDiffEq for PrimitiveIntersections {
    type Epsilon = f64;

    fn default_epsilon() -> f64 {
        f64::default_epsilon()
    }

    fn abs_diff_eq(&self, other: &Self, epsilon: f64) -> bool {
        match (self, other) {
            (Self::One(l), Self::One(r)) => l.abs_diff_eq(r, epsilon),
            (Self::Two(l), Self::Two(r)) => {
                (l[0].abs_diff_eq(&r[0], epsilon) && l[1].abs_diff_eq(&r[1], epsilon))
                    || (l[0].abs_diff_eq(&r[1], epsilon) && l[1].abs_diff_eq(&r[0], epsilon))
            }
            _ => core::mem::discriminant(self) == core::mem::discriminant(other),
        }
    }
}

impl approx::RelativeEq for PrimitiveIntersections {
    fn default_max_relative() -> f64 {
        f64::default_max_relative()
    }

    fn relative_eq(&self, other: &Self, epsilon: f64, max_relative: f64) -> bool {
        match (self, other) {
            (Self::One(l), Self::One(r)) => l.relative_eq(r, epsilon, max_relative),
            (Self::Two(l), Self::Two(r)) => {
                (l[0].relative_eq(&r[0], epsilon, max_relative)
                    && l[1].relative_eq(&r[1], epsilon, max_relative))
                    || (l[0].relative_eq(&r[1], epsilon, max_relative)
                        && l[1].relative_eq(&r[0], epsilon, max_relative))
            }
            _ => core::mem::discriminant(self) == core::mem::discriminant(other),
        }
    }
}

impl approx::UlpsEq for PrimitiveIntersections {
    fn default_max_ulps() -> u32 {
        f64::default_max_ulps()
    }

    fn ulps_eq(&self, other: &Self, epsilon: f64, max_ulps: u32) -> bool {
        match (self, other) {
            (Self::One(l), Self::One(r)) => l.ulps_eq(r, epsilon, max_ulps),
            (Self::Two(l), Self::Two(r)) => {
                (l[0].ulps_eq(&r[0], epsilon, max_ulps) && l[1].ulps_eq(&r[1], epsilon, max_ulps))
                    || (l[0].ulps_eq(&r[1], epsilon, max_ulps)
                        && l[1].ulps_eq(&r[0], epsilon, max_ulps))
            }
            _ => core::mem::discriminant(self) == core::mem::discriminant(other),
        }
    }
}

/**
An iterator over the intersection points in a [`PrimitiveIntersections`] enum.
It is created via the [`IntoIterator::into_iter`] implementation of
[`PrimitiveIntersections`].
 */
pub struct PrimitiveIntersectionsIntoIter {
    intersections: [Option<[f64; 2]>; 2],
    counter: usize,
}

impl Iterator for PrimitiveIntersectionsIntoIter {
    type Item = [f64; 2];

    fn next(&mut self) -> Option<Self::Item> {
        let i = self.intersections.get_mut(self.counter)?.take()?;
        self.counter += 1;
        return Some(i);
    }
}

impl std::ops::Index<usize> for PrimitiveIntersections {
    type Output = [f64; 2];

    fn index(&self, index: usize) -> &Self::Output {
        self.get(index).expect("index is out of bounds")
    }
}

impl PrimitiveIntersections {
    /**
    Returns an iterator over references of all intersections of `self`.

    # Examples

    ```
    use planar_geo::primitive::PrimitiveIntersections;

    assert_eq!(PrimitiveIntersections::Zero.iter().count(), 0);
    assert_eq!(PrimitiveIntersections::One([0.0, 0.0]).iter().count(), 1);
    assert_eq!(PrimitiveIntersections::Two([[0.0, 0.0], [1.0, 0.0]]).iter().count(), 2);
    ```
     */
    pub fn iter(&self) -> PrimitiveIntersectionsIter<'_> {
        return PrimitiveIntersectionsIter {
            intersections: self,
            counter: 0,
        };
    }

    /**
    Returns a reference to the intersection at the given index:
    - If `self` is [`PrimitiveIntersections::Zero`], any index returns [`None`].
    - If `self` is [`PrimitiveIntersections::One`], index 0 returns the contained
    intersection, all other indices return [`None`].
    - If `self` is [`PrimitiveIntersections::Two`], indices 0 and 1 return the respective
    intersection, all other indices return [`None`].

    # Examples

    ```
    use planar_geo::primitive::PrimitiveIntersections;

    let zero = PrimitiveIntersections::Zero;
    let one = PrimitiveIntersections::One([0.0, 0.0]);
    let two = PrimitiveIntersections::Two([[0.0, 0.0], [1.0, 0.0]]);

    assert_eq!(zero.get(0), None);

    assert_eq!(one.get(0), Some(&[0.0, 0.0]));
    assert_eq!(one.get(1), None);

    assert_eq!(two.get(0), Some(&[0.0, 0.0]));
    assert_eq!(two.get(1), Some(&[1.0, 0.0]));
    assert_eq!(two.get(2), None);
    ```
     */
    pub fn get(&self, index: usize) -> Option<&[f64; 2]> {
        match self {
            PrimitiveIntersections::Zero => None,
            PrimitiveIntersections::One(i) => {
                if index == 0 {
                    return Some(i);
                } else {
                    return None;
                }
            }
            PrimitiveIntersections::Two(ai) => ai.get(index),
        }
    }

    /**
    Like [`PrimitiveIntersections::get`], but returns a mutable reference.
     */
    pub fn get_mut(&mut self, index: usize) -> Option<&mut [f64; 2]> {
        match self {
            PrimitiveIntersections::Zero => None,
            PrimitiveIntersections::One(i) => {
                if index == 0 {
                    return Some(i);
                } else {
                    return None;
                }
            }
            PrimitiveIntersections::Two(ai) => ai.get_mut(index),
        }
    }

    /**
    Returns the number of intersections stored in `self`.

    # Examples

    ```
    use planar_geo::primitive::PrimitiveIntersections;

    assert_eq!(PrimitiveIntersections::Zero.len(), 0);
    assert_eq!(PrimitiveIntersections::One([0.0, 0.0]).len(), 1);
    assert_eq!(PrimitiveIntersections::Two([[0.0, 0.0], [1.0, 0.0]]).len(), 2);
    ```
     */
    pub fn len(&self) -> usize {
        match self {
            PrimitiveIntersections::Zero => 0,
            PrimitiveIntersections::One(_) => 1,
            PrimitiveIntersections::Two(_) => 2,
        }
    }

    /**
    Tries to add another intersection to [`PrimitiveIntersections`].

    If `self` is [`PrimitiveIntersections::Two`], no further intersection
    can be added and `false` is returned. Otherwise, `true` is returned
     */
    pub(crate) fn push(&mut self, intersection: [f64; 2]) -> bool {
        match self {
            PrimitiveIntersections::Zero => {
                *self = PrimitiveIntersections::One(intersection);
                return true;
            }
            PrimitiveIntersections::One(i) => {
                *self = PrimitiveIntersections::Two([*i, intersection]);
                return true;
            }
            PrimitiveIntersections::Two(_) => {
                return false;
            }
        }
    }
}

/**
An iterator over the intersection points in a [`PrimitiveIntersections`] enum.
It is created via the [`PrimitiveIntersections::iter`] method.
 */
pub struct PrimitiveIntersectionsIter<'a> {
    intersections: &'a PrimitiveIntersections,
    counter: usize,
}

impl<'a> Iterator for PrimitiveIntersectionsIter<'a> {
    type Item = &'a [f64; 2];

    fn next(&mut self) -> Option<Self::Item> {
        match self.intersections {
            PrimitiveIntersections::Zero => return None,
            PrimitiveIntersections::One(i) => {
                if self.counter == 0 {
                    self.counter += 1;
                    Some(i)
                } else {
                    return None;
                }
            }
            PrimitiveIntersections::Two(ai) => {
                let i = match self.counter {
                    0 => &ai[0],
                    1 => &ai[1],
                    _ => return None,
                };
                self.counter += 1;
                return Some(i);
            }
        }
    }
}

pub(crate) mod private {
    /// Sealed trait for [`Primitive`]
    pub trait Sealed {}
}

/**
A trait for "primitive" geometric types: points (`[f64; 2]`), [`Line`]s,
[`LineSegment`]s and [`ArcSegment`]s.

This trait provides intersection functions between all primitive geometric types
defined in this crate. The simplest way to calculate intersections is the
generic [`Primitive::intersections_primitive`] method. However, specialized
intersection methods for each of the aforementioned types are available as well
and are particularily useful when using [`Primitive`] to define a trait object
(which cannot use generic functions).

By definition, a primitive does not self-intersect, but it does intersect with
equal primitives:

```
use planar_geo::prelude::*;

let ls = LineSegment::new([0.0, 0.0], [1.0, 0.0], DEFAULT_EPSILON, DEFAULT_MAX_ULPS).unwrap();

// Self-intersection
assert_eq!(
    ls.intersections_primitive(&ls, DEFAULT_EPSILON, DEFAULT_MAX_ULPS),
    PrimitiveIntersections::Zero
);

// Intersections with equal primitive
let ls_cloned = ls.clone();
assert_eq!(
    ls.intersections_primitive(&ls_cloned, DEFAULT_EPSILON, DEFAULT_MAX_ULPS),
    PrimitiveIntersections::Two([[0.0, 0.0], [1.0, 0.0]])
);
```

This trait is not meant to be implemented for other types, hence it is
[sealed](https://rust-lang.github.io/api-guidelines/future-proofing.html).
 */
pub trait Primitive: private::Sealed {
    /**
    Returns `true` if `self` contains the given point and `false` otherwise.

    Since floating point values are prone to rounding errors and precision
    issues (i.e. the number 0.2 cannot be represented exactly as a floating
    point number at all), the implementations for the different primitive types
    check whether the point is contained within a tolerance band defined by
    `epsilon` and `max_ulps`. See the [crate-level documentation](crate).

    # Examples

    ```
    use planar_geo::prelude::*;

    let ls = LineSegment::new([0.0, 0.0], [1.0, -1.0], DEFAULT_EPSILON, DEFAULT_MAX_ULPS).unwrap();
    let p1 = [0.5, -0.5];
    let p2 = [0.5, 0.5];
    let p3 = [1.5, 0.5];

    // Default tolerances for "intuitive" behaviour
    assert!(ls.contains_point(p1, DEFAULT_EPSILON, DEFAULT_MAX_ULPS));
    assert!(!ls.contains_point(p2, DEFAULT_EPSILON, DEFAULT_MAX_ULPS));
    assert!(!ls.contains_point(p3, DEFAULT_EPSILON, DEFAULT_MAX_ULPS));

    // By increasing the absolute tolerance `epsilon`, the behaviour of the
    // function is changed
    assert!(ls.contains_point(p1, 10.0, DEFAULT_MAX_ULPS));
    assert!(ls.contains_point(p2, 10.0, DEFAULT_MAX_ULPS));
    assert!(ls.contains_point(p3, 10.0, DEFAULT_MAX_ULPS));
    ```
     */
    fn contains_point(&self, point: [f64; 2], epsilon: f64, max_ulps: u32) -> bool;

    /**
    Returns the intersections between `self` and a point `[f64; 2]`
     *
    This function wraps the given point in [`PrimitiveIntersections::One`] if
    [`Primitive::contains_point`] returned `true` and returns
    [`PrimitiveIntersections::Zero`] otherwise.

    This method is mainly used to implement
    [`Primitive::intersections_primitive`], consider using that method instead.

    # Examples

    ```
    use planar_geo::prelude::*;

    let ls = LineSegment::new([0.0, 0.0], [1.0, -1.0], DEFAULT_EPSILON, DEFAULT_MAX_ULPS).unwrap();
    let p1 = [0.5, -0.5];
    let p2 = [0.5, 0.5];
    let p3 = [1.5, 0.5];

    // Default tolerances for "intuitive" behaviour
    assert_eq!(ls.intersections_point(p1, DEFAULT_EPSILON, DEFAULT_MAX_ULPS), PrimitiveIntersections::One(p1));
    assert_eq!(ls.intersections_point(p2, DEFAULT_EPSILON, DEFAULT_MAX_ULPS), PrimitiveIntersections::Zero);
    assert_eq!(ls.intersections_point(p3, DEFAULT_EPSILON, DEFAULT_MAX_ULPS), PrimitiveIntersections::Zero);

    // By increasing the absolute tolerance `epsilon`, the behaviour of the
    // function is changed
    assert_eq!(ls.intersections_point(p1, 10.0, DEFAULT_MAX_ULPS), PrimitiveIntersections::One(p1));
    assert_eq!(ls.intersections_point(p2, 10.0, DEFAULT_MAX_ULPS), PrimitiveIntersections::One(p2));
    assert_eq!(ls.intersections_point(p3, 10.0, DEFAULT_MAX_ULPS), PrimitiveIntersections::One(p3));
    ```
     */
    fn intersections_point(
        &self,
        point: [f64; 2],
        epsilon: f64,
        max_ulps: u32,
    ) -> PrimitiveIntersections {
        if self.contains_point(point, epsilon, max_ulps) {
            return PrimitiveIntersections::One(point);
        }
        return PrimitiveIntersections::Zero;
    }

    /**
    Returns the intersections between `self` and a [`Line`].

    This is a straightforward calculation, but there are two degenerate cases
    which need to be treated explicitly:
    - Intersection between two [`Line`]s which are identical
    - Intersection between a [`Line`] and [`LineSegment`] where the latter
    is contained in the former.
    In both cases, the returned result is [`PrimitiveIntersections::Zero`] (see
    examples).

    This method is mainly used to implement
    [`Primitive::intersections_primitive`], consider using that method instead.

    # Examples

    ```
    use planar_geo::prelude::*;

    let line_1 = Line::from_point_angle([0.0, 0.0], 1.0);
    let line_2 = Line::from_point_angle([0.0, 0.0], 0.0);
    let ls = LineSegment::new([0.0, 0.0], [1.0, 0.0], DEFAULT_EPSILON, DEFAULT_MAX_ULPS).unwrap();

    // Non-degenerate cases
    assert_eq!(
        line_2.intersections_line(&line_1, DEFAULT_EPSILON, DEFAULT_MAX_ULPS),
        PrimitiveIntersections::One([0.0, 0.0])
    );
    assert_eq!(
        ls.intersections_line(&line_1, DEFAULT_EPSILON, DEFAULT_MAX_ULPS),
        PrimitiveIntersections::One([0.0, 0.0])
    );

    // Degenerate cases
    assert_eq!(
        line_1.intersections_line(&line_1, DEFAULT_EPSILON, DEFAULT_MAX_ULPS),
        PrimitiveIntersections::Zero
    );
    assert_eq!(
        ls.intersections_line(&line_2, DEFAULT_EPSILON, DEFAULT_MAX_ULPS),
        PrimitiveIntersections::Zero
    );
    ```
     */
    fn intersections_line(
        &self,
        line: &Line,
        epsilon: f64,
        max_ulps: u32,
    ) -> PrimitiveIntersections;

    /**
    Returns the intersections between `self` and a [`LineSegment`].

    This is a straightforward calculation, but there are two degenerate cases
    which need to be treated explicitly:
    - Intersection between a [`Line`] and [`LineSegment`] where the latter
    is contained in the former.
    - Intersection between two [`LineSegment`]s which overlap.
    In the former case, the result is defined to be
    [`PrimitiveIntersections::Zero`]. In the latter case, the "common endpoints"
    are returned (see examples).

    This method is mainly used to implement
    [`Primitive::intersections_primitive`], consider using that method instead.

    # Examples

    ```
    use planar_geo::prelude::*;

    let ls1 = LineSegment::new([0.0, 0.0], [2.0, 0.0], DEFAULT_EPSILON, DEFAULT_MAX_ULPS).unwrap();
    let ls2 = LineSegment::new([0.5, 0.5], [0.5, -0.5], DEFAULT_EPSILON, DEFAULT_MAX_ULPS).unwrap();
    let ls3 = LineSegment::new([1.0, 0.0], [1.5, 0.0], DEFAULT_EPSILON, DEFAULT_MAX_ULPS).unwrap();
    let ls4 = LineSegment::new([1.0, 0.0], [3.0, 0.0], DEFAULT_EPSILON, DEFAULT_MAX_ULPS).unwrap();

    // Non-degenerate case
    assert_eq!(
        ls1.intersections_line_segment(&ls2, DEFAULT_EPSILON, DEFAULT_MAX_ULPS),
        PrimitiveIntersections::One([0.5, 0.0])
    );

    // Degenerate cases
    assert_eq!(
        ls1.intersections_line_segment(&ls3, DEFAULT_EPSILON, DEFAULT_MAX_ULPS),
        PrimitiveIntersections::Two([[1.0, 0.0], [1.5, 0.0]])
    );
    assert_eq!(
        ls1.intersections_line_segment(&ls4, DEFAULT_EPSILON, DEFAULT_MAX_ULPS),
        PrimitiveIntersections::Two([[1.0, 0.0], [2.0, 0.0]])
    );
    ```
     */
    fn intersections_line_segment(
        &self,
        line_segment: &LineSegment,
        epsilon: f64,
        max_ulps: u32,
    ) -> PrimitiveIntersections;

    /**
    Returns the intersections between `self` and an [`ArcSegment`].

    Similar to the intersection of two [`LineSegment`]s which overlap (see
    discussion in the docstring of [`Primitive::intersections_line_segment`]),
    this function has to deal with the case of two overlapping [`ArcSegment`]s.
    As with the [`LineSegment`] case, the "common endpoints" are returned (see
    examples).

    This method is mainly used to implement
    [`Primitive::intersections_primitive`], consider using that method instead.

    # Examples

    ```
    use std::f64::consts::PI;
    use planar_geo::prelude::*;

    let arc1 = ArcSegment::from_center_radius_start_offset_angle([0.0, 0.0], 2.0, 0.0, PI, DEFAULT_EPSILON, DEFAULT_MAX_ULPS).unwrap();
    let line = Line::from_point_angle([0.0, 0.0], 0.5 * PI);
    let arc2 = ArcSegment::from_center_radius_start_offset_angle([0.0, 0.0], 2.0, 0.5*PI, PI, DEFAULT_EPSILON, DEFAULT_MAX_ULPS).unwrap();

    // Regular intersection
    approx::assert_abs_diff_eq!(
        line.intersections_arc_segment(&arc1, DEFAULT_EPSILON, DEFAULT_MAX_ULPS),
        PrimitiveIntersections::One([0.0, 2.0]), epsilon = DEFAULT_EPSILON
    );

    // Degenerate case
    approx::assert_abs_diff_eq!(
        arc1.intersections_arc_segment(&arc2, DEFAULT_EPSILON, DEFAULT_MAX_ULPS),
        PrimitiveIntersections::Two([[0.0, 2.0], [-2.0, 0.0]]), epsilon = DEFAULT_EPSILON
    );
    ```
    */
    fn intersections_arc_segment(
        &self,
        arc_segment: &ArcSegment,
        epsilon: f64,
        max_ulps: u32,
    ) -> PrimitiveIntersections;

    /**
    Returns the intersection between `self` and a [`Segment`].

    Since [`Segment`] is an enum of either [`LineSegment`] or [`ArcSegment`],
    this function behaves in the same way as
     [`Primitive::intersections_line_segment`] or
    [`Primitive::intersections_arc_segment`] respectively (especially for
    degenerate cases, see the function docstrings).

    This method is mainly used to implement
    [`Primitive::intersections_primitive`], consider using that method instead.

    # Examples

    ```
    use planar_geo::prelude::*;

    let ls: Segment = LineSegment::new([0.0, 0.0], [2.0, 0.0], DEFAULT_EPSILON, DEFAULT_MAX_ULPS).unwrap().into();
    let arc: Segment = ArcSegment::from_center_radius_start_offset_angle([0.0, 0.0], 1.0, -1.0, 1.0, DEFAULT_EPSILON, DEFAULT_MAX_ULPS).unwrap().into();

    assert_eq!(
        ls.intersections_segment(&arc, DEFAULT_EPSILON, DEFAULT_MAX_ULPS),
        PrimitiveIntersections::One([1.0, 0.0])
    );

    ```
     */
    fn intersections_segment(
        &self,
        segment: &Segment,
        epsilon: f64,
        max_ulps: u32,
    ) -> PrimitiveIntersections {
        match segment {
            Segment::LineSegment(line_segment) => {
                self.intersections_line_segment(line_segment, epsilon, max_ulps)
            }
            Segment::ArcSegment(arc_segment) => {
                self.intersections_arc_segment(arc_segment, epsilon, max_ulps)
            }
        }
    }

    /**
    Returns the intersection between `self` and another type implementing
    [`Primitive`].

    This is a generalized interface to all the special intersection functions.
    For example, if `other` is a [`LineSegment`], the implementation of this
    function boils down to:

    ```ignore
    impl Primitive for LineSegment {
        // Implementations of the other methods ...

        fn intersections_primitive<T: Primitive>(
            &self,
            other: &T,
            epsilon: f64,
            max_ulps: u32,
        ) -> PrimitiveIntersections {
            other.intersections_line_segment(self, epsilon, max_ulps)
        }
    }
    ```

    It is recommended to use this function instead of the specialized methods
    such as [`Primitive::intersections_line_segment`] for primitive intersection
    to simplify the usage of this trait.
     */
    fn intersections_primitive<T: Primitive>(
        &self,
        other: &T,
        epsilon: f64,
        max_ulps: u32,
    ) -> PrimitiveIntersections
    where
        Self: Sized;

    /**
    Returns the intersections between a [`Primitive`] and any other geometric
    type.

    This method is based on
    [`GeometryRef::intersections`](crate::geometry::GeometryRef::intersections).
    It can handle any geometric type ([`Primitive`] or
    [`Composite`](crate::composite::Composite)) defined in this crate, but it
    needs to allocate a vector for the results. For intersections between two
    primitives, [`Primitive::intersections_primitive`] is therefore to be
     preferred. If `other` is a [`Composite`](crate::composite::Composite), the
    [`Composite::intersections_primitive`](crate::composite::Composite::intersections_primitive)
    method offers a non-allocating (lazy) alternative.

    The main advantage of this method is its genericness. If allocating a vector
    for the results is not an issue, consider using this method instead of the
    specialized variants mentioned above to simplify the interface to this
    trait.

    # Examples

    ```
    use std::f64::consts::PI;
    use planar_geo::prelude::*;

    let arc1 = ArcSegment::from_center_radius_start_offset_angle([0.0, 0.0], 2.0, 0.0, PI, DEFAULT_EPSILON, DEFAULT_MAX_ULPS).unwrap();
    let arc2 = ArcSegment::from_center_radius_start_offset_angle([0.0, 0.0], 2.0, 0.5*PI, PI, DEFAULT_EPSILON, DEFAULT_MAX_ULPS).unwrap();

    let intersections = arc1.intersections(&arc2, DEFAULT_EPSILON, DEFAULT_MAX_ULPS);
    assert_eq!(intersections.len(), 2);
    approx::assert_abs_diff_eq!(intersections[0].point, [-2.0, 0.0], epsilon = DEFAULT_EPSILON);
    approx::assert_abs_diff_eq!(intersections[1].point, [0.0, 2.0], epsilon = DEFAULT_EPSILON);

    // Comparison to specialized function:
    approx::assert_abs_diff_eq!(
        arc1.intersections_primitive(&arc2, DEFAULT_EPSILON, DEFAULT_MAX_ULPS),
        PrimitiveIntersections::Two([[0.0, 2.0], [-2.0, 0.0]]), epsilon = DEFAULT_EPSILON
    );
    ```
     */
    fn intersections<'a, T: Into<crate::geometry::GeometryRef<'a>>>(
        &self,
        other: T,
        epsilon: f64,
        max_ulps: u32,
    ) -> Vec<crate::composite::Intersection>
    where
        Self: Sized,
    {
        let geo_ref: crate::geometry::GeometryRef = other.into();
        return geo_ref.intersections_primitive(self, epsilon, max_ulps);
    }
}

impl private::Sealed for [f64; 2] {}

impl Primitive for [f64; 2] {
    fn contains_point(&self, point: [f64; 2], epsilon: f64, max_ulps: u32) -> bool {
        return self.ulps_eq(&point, epsilon, max_ulps);
    }

    fn intersections_line(
        &self,
        line: &Line,
        epsilon: f64,
        max_ulps: u32,
    ) -> PrimitiveIntersections {
        line.intersections_point(*self, epsilon, max_ulps)
    }

    fn intersections_line_segment(
        &self,
        line_segment: &LineSegment,
        epsilon: f64,
        max_ulps: u32,
    ) -> PrimitiveIntersections {
        line_segment.intersections_point(*self, epsilon, max_ulps)
    }

    fn intersections_arc_segment(
        &self,
        arc_segment: &ArcSegment,
        epsilon: f64,
        max_ulps: u32,
    ) -> PrimitiveIntersections {
        arc_segment.intersections_point(*self, epsilon, max_ulps)
    }

    fn intersections_primitive<T: Primitive>(
        &self,
        other: &T,
        epsilon: f64,
        max_ulps: u32,
    ) -> PrimitiveIntersections {
        other.intersections_point(*self, epsilon, max_ulps)
    }
}