pub const RECT_ZERO: Rect = Rect {
top: 0,
right: 0,
bottom: 0,
left: 0,
};
pub type Rectangles = Vec<Rect>;
#[derive(Copy, Clone, PartialEq)]
pub struct Rect {
pub left: usize,
pub top: usize,
pub right: usize,
pub bottom: usize,
}
impl Rect {
pub fn new(left: usize, top: usize, right: usize, bottom: usize) -> Rect {
Rect { left, top, right, bottom }
}
pub fn inc_top(&self, offset: usize) -> Rect {
Rect {
left: self.left,
top: self.top + offset,
right: self.right,
bottom: self.bottom,
}
}
pub fn unpack(&self) -> (usize, usize, usize, usize) {
(self.left, self.top, self.right, self.bottom)
}
pub fn contains(&self, r: &Rect) -> bool {
r.left >= self.left && r.top >= self.top && r.right <= self.right && r.bottom <= self.bottom
}
pub fn width(&self) -> usize {
self.right - self.left
}
pub fn height(&self) -> usize {
self.bottom - self.top
}
}
impl std::fmt::Display for Rect {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "({},{};{},{})", self.left, self.top, self.right, self.bottom)
}
}
impl std::fmt::Debug for Rect {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "({},{};{},{})", self.left, self.top, self.right, self.bottom)
}
}