geo/algorithm/intersects/
rect.rs

1use super::Intersects;
2use crate::*;
3
4impl<T> Intersects<Coord<T>> for Rect<T>
5where
6    T: CoordNum,
7{
8    fn intersects(&self, rhs: &Coord<T>) -> bool {
9        rhs.x >= self.min().x
10            && rhs.y >= self.min().y
11            && rhs.x <= self.max().x
12            && rhs.y <= self.max().y
13    }
14}
15
16symmetric_intersects_impl!(Rect<T>, LineString<T>);
17symmetric_intersects_impl!(Rect<T>, MultiLineString<T>);
18
19// Same logic as Polygon<T>: Intersects<Line<T>>, but avoid
20// an allocation.
21impl<T> Intersects<Line<T>> for Rect<T>
22where
23    T: GeoNum,
24{
25    fn intersects(&self, rhs: &Line<T>) -> bool {
26        let lb = self.min();
27        let rt = self.max();
28        let lt = Coord::from((lb.x, rt.y));
29        let rb = Coord::from((rt.x, lb.y));
30        // If either rhs.{start,end} lies inside Rect, then true
31        self.intersects(&rhs.start)
32            || self.intersects(&rhs.end)
33            || Line::new(lt, rt).intersects(rhs)
34            || Line::new(rt, rb).intersects(rhs)
35            || Line::new(lb, rb).intersects(rhs)
36            || Line::new(lt, lb).intersects(rhs)
37    }
38}
39
40symmetric_intersects_impl!(Rect<T>, Point<T>);
41symmetric_intersects_impl!(Rect<T>, MultiPoint<T>);
42
43symmetric_intersects_impl!(Rect<T>, Polygon<T>);
44symmetric_intersects_impl!(Rect<T>, MultiPolygon<T>);
45
46impl<T> Intersects<Rect<T>> for Rect<T>
47where
48    T: CoordNum,
49{
50    fn intersects(&self, other: &Rect<T>) -> bool {
51        if self.max().x < other.min().x {
52            return false;
53        }
54
55        if self.max().y < other.min().y {
56            return false;
57        }
58
59        if self.min().x > other.max().x {
60            return false;
61        }
62
63        if self.min().y > other.max().y {
64            return false;
65        }
66
67        true
68    }
69}
70
71impl<T> Intersects<Triangle<T>> for Rect<T>
72where
73    T: GeoNum,
74{
75    fn intersects(&self, rhs: &Triangle<T>) -> bool {
76        self.intersects(&rhs.to_polygon())
77    }
78}