use std::fmt::Display;
use crate::{Position, Size};
#[derive(Debug, Default, Clone, Copy, PartialEq, PartialOrd)]
pub struct Rect {
position: Position,
size: Size,
}
impl Rect {
#[must_use]
pub const fn new(position: Position, size: Size) -> Self {
Self {
position,
size,
}
}
#[must_use]
pub const fn position(&self) -> Position {
self.position
}
#[must_use]
pub fn middle(&self) -> Position {
Position::new(
self.x() + self.width() / 2.0,
self.y() + self.height() / 2.0
)
}
#[must_use]
pub const fn x(&self) -> f64 {
self.position().x()
}
#[must_use]
pub const fn y(&self) -> f64 {
self.position().y()
}
#[must_use]
pub const fn size(&self) -> Size {
self.size
}
#[must_use]
pub const fn width(&self) -> f64 {
self.size().width()
}
#[must_use]
pub const fn height(&self) -> f64 {
self.size().height()
}
}
impl From<(Position, Size)> for Rect {
fn from(value: (Position, Size)) -> Self {
Self::new(value.0, value.1)
}
}
impl Display for Rect {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("rectangle at {} sized {}", self.position, self.size))
}
}