use math::Vec2;
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct Bounds {
min: Vec2,
max: Vec2,
}
impl Bounds {
pub fn new(min: Vec2, max: Vec2) -> Bounds {
Bounds {
min,
max,
}
}
pub fn center_extents(center: Vec2, extents: Vec2) -> Bounds {
Bounds::new(center - extents, center + extents)
}
#[inline]
pub fn perimeter(&self) -> f32 {
2.0 * (self.max.x - self.min.x + self.max.y - self.min.y)
}
#[inline]
pub fn intersects(&self, other: &Bounds) -> bool {
if self.max.x < other.min.x || self.min.x > other.max.x {
false
} else if self.max.y < other.min.y || self.min.y > other.max.y {
false
} else {
true
}
}
#[inline]
pub fn contains(&self, other: &Bounds) -> bool {
self.min.min(&other.min) == self.min && self.max.max(&other.max) == self.max
}
pub fn union(&self, other: &Bounds) -> Bounds {
Bounds::new(self.min.min(&other.min), self.max.max(&other.max))
}
pub fn expand(&self, margin: f32) -> Bounds {
Bounds::new(self.min - Vec2::ONE * margin, self.max + Vec2::ONE * margin)
}
}