1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- 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<Self>;
- }
- 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<Coord>;
- }
- 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<Coord>) -> 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
- }
- }
|