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
use super::EndPoint;
use super::ILineSegment;
use super::LineSegment;
use super::LineSegmentView;
use super::Point;
use crate::{Intersects, PolygonScalar};

#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
// Directed edge from A to B, including A and excluding B.
pub struct DirectedEdge<T, const N: usize> {
  pub src: Point<T, N>,
  pub dst: Point<T, N>,
}

impl<T> DirectedEdge<T, 2> {
  pub fn contains(&self, pt: &Point<T, 2>) -> bool
  where
    T: Ord + PartialOrd,
  {
    LineSegmentView::from(self).contains(pt)
  }
}

impl<T: Ord, const N: usize> From<DirectedEdge<T, N>> for LineSegment<T, N> {
  fn from(edge: DirectedEdge<T, N>) -> LineSegment<T, N> {
    LineSegment::new(EndPoint::Inclusive(edge.src), EndPoint::Exclusive(edge.dst))
  }
}

impl<'a, T: Ord, const N: usize> From<&'a DirectedEdge<T, N>> for LineSegmentView<'a, T, N> {
  fn from(edge: &'a DirectedEdge<T, N>) -> LineSegmentView<'a, T, N> {
    LineSegmentView::new(
      EndPoint::Inclusive(&edge.src),
      EndPoint::Exclusive(&edge.dst),
    )
  }
}

impl<'a, T> Intersects for &'a DirectedEdge<T, 2>
where
  T: PolygonScalar,
{
  type Result = ILineSegment<'a, T>;
  fn intersect(self, other: &'a DirectedEdge<T, 2>) -> Option<Self::Result> {
    LineSegmentView::from(self).intersect(LineSegmentView::from(other))
  }
}