use crate::{Radians, Vector};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
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,
}
}
pub fn rotate(self, rotation: Radians) -> Size {
let radians = f32::from(rotation);
Size {
width: (self.width * radians.cos()).abs()
+ (self.height * radians.sin()).abs(),
height: (self.width * radians.sin()).abs()
+ (self.height * radians.cos()).abs(),
}
}
}
impl<T> From<[T; 2]> for Size<T> {
fn from([width, height]: [T; 2]) -> Self {
Size { width, height }
}
}
impl<T> From<(T, T)> for Size<T> {
fn from((width, height): (T, T)) -> Self {
Self { width, height }
}
}
impl<T> From<Vector<T>> for Size<T> {
fn from(vector: Vector<T>) -> Self {
Size {
width: vector.x,
height: vector.y,
}
}
}
impl<T> From<Size<T>> for [T; 2] {
fn from(size: Size<T>) -> Self {
[size.width, size.height]
}
}
impl<T> From<Size<T>> for Vector<T> {
fn from(size: Size<T>) -> Self {
Vector::new(size.width, size.height)
}
}
impl<T> std::ops::Add for Size<T>
where
T: std::ops::Add<Output = T>,
{
type Output = Size<T>;
fn add(self, rhs: Self) -> Self::Output {
Size {
width: self.width + rhs.width,
height: self.height + rhs.height,
}
}
}
impl<T> std::ops::Sub for Size<T>
where
T: std::ops::Sub<Output = T>,
{
type Output = Size<T>;
fn sub(self, rhs: Self) -> Self::Output {
Size {
width: self.width - rhs.width,
height: self.height - rhs.height,
}
}
}
impl<T> std::ops::Mul<T> for Size<T>
where
T: std::ops::Mul<Output = T> + Copy,
{
type Output = Size<T>;
fn mul(self, rhs: T) -> Self::Output {
Size {
width: self.width * rhs,
height: self.height * rhs,
}
}
}
impl<T> std::ops::Mul<Vector<T>> for Size<T>
where
T: std::ops::Mul<Output = T> + Copy,
{
type Output = Size<T>;
fn mul(self, scale: Vector<T>) -> Self::Output {
Size {
width: self.width * scale.x,
height: self.height * scale.y,
}
}
}