haar_lib/geom/
intersect_segments.rs1use crate::geom::{ccw::*, *};
4
5#[derive(Clone, Copy, Debug, PartialEq)]
7#[allow(non_camel_case_types)]
8pub enum IntersectSegments {
9 INTERSECTED,
11 OVERLAPPED,
13 NOT_INTERSECTED,
15 SAME,
17}
18
19impl IntersectSegments {
20 pub fn intersected(self) -> bool {
22 self == Self::INTERSECTED
23 }
24 pub fn overlapped(self) -> bool {
26 self == Self::OVERLAPPED
27 }
28 pub fn not_intersected(self) -> bool {
30 self == Self::NOT_INTERSECTED
31 }
32 pub fn same(self) -> bool {
34 self == Self::SAME
35 }
36}
37
38pub fn intersect_segments(a: Line, b: Line, eps: Eps) -> (IntersectSegments, Option<Vector>) {
40 use self::IntersectSegments::*;
41
42 let cr = a.cross(b);
43
44 if eps.eq(cr.abs(), 0.0) {
45 return if ccw(a.from, a.to, b.from, eps).to_value()
46 * ccw(a.from, a.to, b.to, eps).to_value()
47 <= 0
48 && ccw(b.from, b.to, a.from, eps).to_value() * ccw(b.from, b.to, a.to, eps).to_value()
49 <= 0
50 {
51 (OVERLAPPED, None)
52 } else {
53 (NOT_INTERSECTED, None)
54 };
55 }
56
57 let t1 = (b.from - a.from).cross(b.diff()) / cr;
58 let t2 = (b.from - a.from).cross(a.diff()) / cr;
59
60 if eps.lt(t1, 0.0) || eps.gt(t1, 1.0) || eps.lt(t2, 0.0) || eps.gt(t2, 1.0) {
61 (NOT_INTERSECTED, None)
62 } else {
63 (INTERSECTED, Some(a.from + a.diff() * t1))
64 }
65}