use crate::*; use std::fmt; #[derive(Copy, Clone, PartialEq)] pub struct Dimensions<D: Internal> { pub width: D, pub height: D, } pub trait HasDimensions<D: Internal> { fn dimensions(&self) -> Dimensions<D>; fn width(&self) -> D { self.dimensions().width } fn height(&self) -> D { self.dimensions().height } fn area_usize(&self) -> usize { self.dimensions().width.as_usize() * self.dimensions().height.as_usize() } fn area_f64(&self) -> f64 { self.dimensions().width.as_f64() * self.dimensions().height.as_f64() } fn contains_point(&self, point: Point<D>) -> bool { point.x < self.width() && point.y < self.height() } } impl<D: Internal> Dimensions<D> { pub const ZERO: Self = Self { width: D::ZERO, height: D::ZERO }; pub const fn new(width: D, height: D) -> Self { Dimensions { width, height } } pub fn is_zero(&self) -> bool { self.width == D::ZERO && self.height == D::ZERO } } impl<D: Internal> HasDimensions<D> for Dimensions<D> { fn dimensions(&self) -> Dimensions<D> { *self } } impl<O: Internal, D: Internal> From<Rect<O, D>> for Dimensions<D> { fn from(rect: Rect<O, D>) -> Self { rect.dimensions } } impl<D: Internal> fmt::Debug for Dimensions<D> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Dimensions {{ w:{:?} h:{:?} }}", self.width, self.height) } } impl<D: Internal> std::ops::Mul<D> for Dimensions<D> { type Output = Self; fn mul(mut self, scale: D) -> Self { self.width *= scale; self.height *= scale; return self; } } impl<D: Internal> std::ops::MulAssign<D> for Dimensions<D> { fn mul_assign(&mut self, scale: D) { self.width *= scale; self.height *= scale; } } impl<D: Internal> std::ops::Div<D> for Dimensions<D> { type Output = Self; fn div(mut self, scale: D) -> Self { self.width /= scale; self.height /= scale; return self; } } impl<D: Internal> std::ops::DivAssign<D> for Dimensions<D> { fn div_assign(&mut self, scale: D) { self.width /= scale; self.height /= scale; } }