Skip to content

Commit

Permalink
Make the union of two rectangles more precise if one of them is
Browse files Browse the repository at this point in the history
zero-sized.
  • Loading branch information
pcwalton committed May 22, 2015
1 parent c4bdb1e commit 62b6f4e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub fn Rect<T:Clone>(origin: Point2D<T>, size: Size2D<T>) -> Rect<T> {
}
}

impl<T: Copy + Clone + PartialOrd + Add<T, Output=T> + Sub<T, Output=T>> Rect<T> {
impl<T: Copy + Clone + PartialOrd + Add<T, Output=T> + Sub<T, Output=T> + Zero> Rect<T> {
#[inline]
pub fn intersects(&self, other: &Rect<T>) -> bool {
self.origin.x < other.origin.x + other.size.width &&
Expand Down Expand Up @@ -89,6 +89,13 @@ impl<T: Copy + Clone + PartialOrd + Add<T, Output=T> + Sub<T, Output=T>> Rect<T>

#[inline]
pub fn union(&self, other: &Rect<T>) -> Rect<T> {
if self.size == Zero::zero() {
return *other
}
if other.size == Zero::zero() {
return *self
}

let upper_left = Point2D(min(self.min_x(), other.min_x()),
min(self.min_y(), other.min_y()));

Expand Down
9 changes: 9 additions & 0 deletions src/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ impl<T: Zero> Size2D<T> {
}
}

impl<T: Zero> Zero for Size2D<T> {
fn zero() -> Size2D<T> {
Size2D {
width: Zero::zero(),
height: Zero::zero(),
}
}
}

impl<Scale: Copy, T0: Mul<Scale, Output=T1>, T1: Clone> Mul<Scale> for Size2D<T0> {
type Output = Size2D<T1>;
#[inline]
Expand Down

0 comments on commit 62b6f4e

Please sign in to comment.