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
use super::{point_in_rect, Intersects};
use crate::kernels::*;
use crate::*;
impl<T> Intersects<Coordinate<T>> for Line<T>
where
T: HasKernel,
{
fn intersects(&self, rhs: &Coordinate<T>) -> bool {
T::Ker::orient2d(self.start, self.end, *rhs) == Orientation::Collinear
&& point_in_rect(*rhs, self.start, self.end)
}
}
symmetric_intersects_impl!(Coordinate<T>, Line<T>);
symmetric_intersects_impl!(Line<T>, Point<T>);
impl<T> Intersects<Line<T>> for Line<T>
where
T: HasKernel,
{
fn intersects(&self, line: &Line<T>) -> bool {
if self.start == self.end {
return line.intersects(&self.start);
}
let check_1_1 = T::Ker::orient2d(self.start, self.end, line.start);
let check_1_2 = T::Ker::orient2d(self.start, self.end, line.end);
if check_1_1 != check_1_2 {
let check_2_1 = T::Ker::orient2d(line.start, line.end, self.start);
let check_2_2 = T::Ker::orient2d(line.start, line.end, self.end);
check_2_1 != check_2_2
} else if check_1_1 == Orientation::Collinear {
point_in_rect(line.start, self.start, self.end)
|| point_in_rect(line.end, self.start, self.end)
|| point_in_rect(self.end, line.start, line.end)
|| point_in_rect(self.end, line.start, line.end)
} else {
false
}
}
}