geo/algorithm/contains/
rect.rs

1use super::{impl_contains_from_relate, impl_contains_geometry_for, Contains};
2use crate::geometry::*;
3use crate::{CoordNum, GeoFloat};
4
5// ┌──────────────────────────┐
6// │ Implementations for Rect │
7// └──────────────────────────┘
8
9impl<T> Contains<Coord<T>> for Rect<T>
10where
11    T: CoordNum,
12{
13    fn contains(&self, coord: &Coord<T>) -> bool {
14        coord.x > self.min().x
15            && coord.x < self.max().x
16            && coord.y > self.min().y
17            && coord.y < self.max().y
18    }
19}
20
21impl<T> Contains<Point<T>> for Rect<T>
22where
23    T: CoordNum,
24{
25    fn contains(&self, p: &Point<T>) -> bool {
26        self.contains(&p.0)
27    }
28}
29
30impl<T> Contains<Rect<T>> for Rect<T>
31where
32    T: CoordNum,
33{
34    fn contains(&self, other: &Rect<T>) -> bool {
35        // TODO: check for degenerate rectangle (which is a line or a point)
36        // All points of LineString must be in the polygon ?
37        self.min().x <= other.min().x
38            && self.max().x >= other.max().x
39            && self.min().y <= other.min().y
40            && self.max().y >= other.max().y
41    }
42}
43
44impl_contains_from_relate!(Rect<T>, [Line<T>, LineString<T>, Polygon<T>, MultiPoint<T>, MultiLineString<T>, MultiPolygon<T>, GeometryCollection<T>, Triangle<T>]);
45impl_contains_geometry_for!(Rect<T>);