use std::cmp::{min, max}; #[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)] pub struct Size { pub width: usize, pub height: usize, } impl specs::Component for Size { type Storage = specs::VecStorage; } impl From<[usize; 2]> for Size { fn from([width, height]: [usize; 2]) -> Size { Size { width, height } } } #[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)] pub struct Coord { pub x: usize, pub y: usize, } impl specs::Component for Coord { type Storage = specs::VecStorage; } impl From<[usize; 2]> for Coord { fn from([x, y]: [usize; 2]) -> Coord { Coord { x, y } } } #[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)] pub struct Rect { pub origin: Coord, pub size: Size, } impl Rect { pub fn new(origin: Coord, size: Size) -> Rect{ Rect { origin, size } } pub fn from_points(p1: Coord, p2: Coord) -> Rect { let origin = Coord { x: min(p1.x, p2.x), y: min(p1.y, p2.y), }; let size = Size { width: max(p1.x, p2.x) - origin.x, height: max(p1.y, p2.y) - origin.y, }; Rect { origin, size } } pub fn area(&self) -> usize { self.size.width * self.size.height } pub fn width(&self) -> usize { self.size.width } pub fn height(&self) -> usize { self.size.height } pub fn x(&self) -> usize { self.origin.x } pub fn y(&self) -> usize { self.origin.y } pub fn contains(&self, pt: impl Into) -> bool { let pt = pt.into(); pt.x >= self.origin.x && pt.y >= self.origin.y && pt.x < self.origin.x + self.size.width && pt.y < self.origin.y + self.size.height } }