From 5d8ac82688761c526a5dd3b1d7b9229ccafb1ce5 Mon Sep 17 00:00:00 2001 From: Victor Brekenfeld Date: Mon, 20 Dec 2021 19:09:58 +0100 Subject: [PATCH] geometry: Don't compute intersection for non-overlapping rects --- src/utils/geometry.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/utils/geometry.rs b/src/utils/geometry.rs index 0018de6..dc62786 100644 --- a/src/utils/geometry.rs +++ b/src/utils/geometry.rs @@ -886,16 +886,21 @@ impl Rectangle { } /// Clamp rectangle to min and max corners resulting in the overlapping area of two rectangles + /// + /// Returns `None` if the two rectangles don't overlap #[inline] - pub fn intersection(self, other: impl Into>) -> Self { + pub fn intersection(self, other: impl Into>) -> Option { let other = other.into(); - Rectangle::from_extemities( + if !self.overlaps(other) { + return None; + } + Some(Rectangle::from_extemities( (self.loc.x.max(other.loc.x), self.loc.y.max(other.loc.y)), ( (self.loc.x.saturating_add(self.size.w)).min(other.loc.x.saturating_add(other.size.w)), (self.loc.y.saturating_add(self.size.h)).min(other.loc.y.saturating_add(other.size.h)), ), - ) + )) } /// Compute the bounding box of a given set of points