27 lines
650 B
Rust
27 lines
650 B
Rust
use crate::vec2::Vec2;
|
|
use num::{Num, ToPrimitive};
|
|
|
|
#[derive(Copy, Clone)]
|
|
pub struct Rect<T: Num + Copy + ToPrimitive + PartialOrd> {
|
|
pub pos: Vec2<T>,
|
|
pub size: Vec2<T>,
|
|
}
|
|
|
|
impl<T: Num + Copy + ToPrimitive + PartialOrd> Rect<T> {
|
|
pub fn new(x: T, y: T, w: T, h: T) -> Rect<T> {
|
|
Rect {
|
|
pos: Vec2 { x, y },
|
|
size: Vec2 { x: w, y: h },
|
|
}
|
|
}
|
|
|
|
pub fn pos2(&self) -> Vec2<T> {
|
|
self.pos + self.size
|
|
}
|
|
|
|
pub fn contains(&self, point: Vec2<T>) -> bool {
|
|
let p2 = self.pos2();
|
|
point.x >= self.pos.x && point.y >= self.pos.y && point.x < p2.x && point.y < p2.y
|
|
}
|
|
}
|