Skip to content

Commit

Permalink
Don't include empty boxes in their union.
Browse files Browse the repository at this point in the history
The new behaviour matches what Rect does.
  • Loading branch information
nical committed May 28, 2021
1 parent f271116 commit e35f9dc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
10 changes: 10 additions & 0 deletions src/box2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
11 changes: 10 additions & 1 deletion src/box3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
7 changes: 0 additions & 7 deletions src/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down

0 comments on commit e35f9dc

Please sign in to comment.