diff --git a/src/box2d.rs b/src/box2d.rs index 36a4e3b..46a0d7b 100644 --- a/src/box2d.rs +++ b/src/box2d.rs @@ -213,8 +213,18 @@ where } } + /// Computes the union of two boxes. + /// + /// If either of the boxes is empty, the other one is returned. #[inline] pub fn union(&self, other: &Self) -> Self { + if other.is_empty() { + return *self; + } + if self.is_empty() { + return *other; + } + Box2D { min: point2(min(self.min.x, other.min.x), min(self.min.y, other.min.y)), max: point2(max(self.max.x, other.max.x), max(self.max.y, other.max.y)), diff --git a/src/box3d.rs b/src/box3d.rs index 7a5d9cd..38d0594 100644 --- a/src/box3d.rs +++ b/src/box3d.rs @@ -183,9 +183,18 @@ where Box3D::new(intersection_min, intersection_max) } - /// Returns the smallest box containing both of the provided boxes. + /// Computes the union of two boxes. + /// + /// If either of the boxes is empty, the other one is returned. #[inline] pub fn union(&self, other: &Self) -> Self { + if other.is_empty() { + return *self; + } + if self.is_empty() { + return *other; + } + Box3D::new( Point3D::new( min(self.min.x, other.min.x), diff --git a/src/rect.rs b/src/rect.rs index 289ff7a..9cecfdc 100644 --- a/src/rect.rs +++ b/src/rect.rs @@ -368,13 +368,6 @@ where { #[inline] pub fn union(&self, other: &Self) -> Self { - if self.size == Zero::zero() { - return *other; - } - if other.size == Zero::zero() { - return *self; - } - self.to_box2d().union(&other.to_box2d()).to_rect() } }