haar_lib/geom/
intersect_line_segment.rs1use crate::geom::*;
4use std::cmp::Ordering::*;
5
6#[derive(Clone, Copy, Debug, PartialEq)]
8#[allow(non_camel_case_types)]
9pub enum IntersectLineSegment {
10 LEFTSIDE,
12 RIGHTSIDE,
14 OVERLAPPED,
16 CROSSED,
18}
19
20impl IntersectLineSegment {
21 pub fn leftside(self) -> bool {
23 self == Self::LEFTSIDE
24 }
25 pub fn rightside(self) -> bool {
27 self == Self::RIGHTSIDE
28 }
29 pub fn overlapped(self) -> bool {
31 self == Self::OVERLAPPED
32 }
33 pub fn crossed(self) -> bool {
35 self == Self::CROSSED
36 }
37}
38
39pub fn intersect_line_segment(
41 l: Line,
42 s: Line,
43 eps: Eps,
44) -> (IntersectLineSegment, Option<Vector>) {
45 use self::IntersectLineSegment::*;
46
47 let a = l.diff().cross(s.from - l.from);
48 let b = l.diff().cross(s.to - l.from);
49
50 match (eps.partial_cmp(a, 0.0), eps.partial_cmp(b, 0.0)) {
51 (Some(Equal), Some(Equal)) => (OVERLAPPED, None),
52 (Some(Less), Some(Less)) => (RIGHTSIDE, None),
53 (Some(Greater), Some(Greater)) => (LEFTSIDE, None),
54 _ => (
55 CROSSED,
56 Some(s.from + s.diff() * l.diff().cross(l.from - s.from) / l.cross(s)),
57 ),
58 }
59}