utils: Add intersection to Rectangle

This commit is contained in:
Victor Brekenfeld 2021-12-16 16:55:22 +01:00
parent 1a7027eefc
commit 4628fc6bcc
1 changed files with 13 additions and 0 deletions

View File

@ -833,6 +833,19 @@ impl<N: Coordinate, Kind> Rectangle<N, Kind> {
) )
} }
/// Clamp rectangle to min and max corners resulting in the overlapping area of two rectangles
#[inline]
pub fn intersection(self, other: impl Into<Rectangle<N, Kind>>) -> Self {
let other = other.into();
Rectangle::from_extemities(
(self.loc.x.max(other.loc.x), self.loc.y.max(other.loc.y)),
(
(self.loc.x + self.size.w).min(other.loc.x + other.size.w),
(self.loc.y + self.size.h).min(other.loc.y + other.size.h),
),
)
}
/// Compute the bounding box of a given set of points /// Compute the bounding box of a given set of points
pub fn bounding_box(points: impl IntoIterator<Item = Point<N, Kind>>) -> Self { pub fn bounding_box(points: impl IntoIterator<Item = Point<N, Kind>>) -> Self {
let ret = points.into_iter().fold(None, |acc, point| match acc { let ret = points.into_iter().fold(None, |acc, point| match acc {