use std::fmt;
use std::fmt::{Debug, Display};
#[derive(Default, Copy, Clone, PartialEq, Eq)]
pub struct Rect {
pub left: usize,
pub top: usize,
pub right: usize,
pub bottom: usize,
}
impl Display for Rect {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({},{};{},{})", self.left, self.top, self.right, self.bottom)
}
}
impl Debug for Rect {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self)
}
}
impl Rect {
pub fn new(left: usize, top: usize, right: usize, bottom: usize) -> Self {
Self { left, top, right, bottom }
}
pub fn offset_top(&self, offset: usize) -> Self {
Self { top: self.top + offset, ..*self }
}
pub fn contains(&self, other: &Rect) -> bool {
other.left >= self.left && other.top >= self.top && other.right <= self.right && other.bottom <= self.bottom
}
pub fn width(&self) -> usize {
self.right - self.left
}
pub fn height(&self) -> usize {
self.bottom - self.top
}
}
impl From<Rect> for (usize, usize, usize, usize) {
fn from(value: Rect) -> Self {
(value.left, value.top, value.right, value.bottom)
}
}