use crate::Vector;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Size<T = f32> {
pub width: T,
pub height: T,
}
impl<T> Size<T> {
pub const fn new(width: T, height: T) -> Self {
Size { width, height }
}
}
impl Size {
pub const ZERO: Size = Size::new(0., 0.);
pub const UNIT: Size = Size::new(1., 1.);
pub const INFINITY: Size = Size::new(f32::INFINITY, f32::INFINITY);
pub fn min(self, other: Self) -> Self {
Size {
width: self.width.min(other.width),
height: self.height.min(other.height),
}
}
pub fn max(self, other: Self) -> Self {
Size {
width: self.width.max(other.width),
height: self.height.max(other.height),
}
}
pub fn expand(self, other: impl Into<Size>) -> Self {
let other = other.into();
Size {
width: self.width + other.width,
height: self.height + other.height,
}
}
}
impl From<[f32; 2]> for Size {
fn from([width, height]: [f32; 2]) -> Self {
Size { width, height }
}
}
impl From<[u16; 2]> for Size {
fn from([width, height]: [u16; 2]) -> Self {
Size::new(width.into(), height.into())
}
}
impl From<Vector<f32>> for Size {
fn from(vector: Vector<f32>) -> Self {
Size {
width: vector.x,
height: vector.y,
}
}
}
impl From<Size> for [f32; 2] {
fn from(size: Size) -> [f32; 2] {
[size.width, size.height]
}
}
impl From<Size> for Vector<f32> {
fn from(size: Size) -> Self {
Vector::new(size.width, size.height)
}
}
impl std::ops::Sub for Size {
type Output = Size;
fn sub(self, rhs: Self) -> Self::Output {
Size {
width: self.width - rhs.width,
height: self.height - rhs.height,
}
}
}