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
use super::{value_in_range, Intersects};
use crate::*;
impl<T> Intersects<Coordinate<T>> for Rect<T>
where
T: CoordNum,
{
fn intersects(&self, rhs: &Coordinate<T>) -> bool {
let bound_1 = self.min();
let bound_2 = self.max();
value_in_range(rhs.x, bound_1.x, bound_2.x) && value_in_range(rhs.y, bound_1.y, bound_2.y)
}
}
symmetric_intersects_impl!(Coordinate<T>, Rect<T>);
symmetric_intersects_impl!(Rect<T>, Point<T>);
symmetric_intersects_impl!(Rect<T>, MultiPoint<T>);
impl<T> Intersects<Rect<T>> for Rect<T>
where
T: CoordNum,
{
fn intersects(&self, other: &Rect<T>) -> bool {
let x_overlap = value_in_range(self.min().x, other.min().x, other.max().x)
|| value_in_range(other.min().x, self.min().x, self.max().x);
let y_overlap = value_in_range(self.min().y, other.min().y, other.max().y)
|| value_in_range(other.min().y, self.min().y, self.max().y);
x_overlap && y_overlap
}
}
impl<T> Intersects<Line<T>> for Rect<T>
where
T: GeoNum,
{
fn intersects(&self, rhs: &Line<T>) -> bool {
let lt = self.min();
let rb = self.max();
let lb = Coordinate::from((lt.x, rb.y));
let rt = Coordinate::from((rb.x, lt.y));
self.intersects(&rhs.start)
|| self.intersects(&rhs.end)
|| Line::new(lt, rt).intersects(rhs)
|| Line::new(rt, rb).intersects(rhs)
|| Line::new(lb, rb).intersects(rhs)
|| Line::new(lt, lb).intersects(rhs)
}
}
symmetric_intersects_impl!(Line<T>, Rect<T>);