#[derive(Clone, Copy, Debug)]
pub struct Vec2{
pub x: i32,
pub y: i32
}
impl Vec2 {
pub fn new(x: i32, y: i32) -> Vec2{
Vec2 { x, y }
}
pub fn as_u32_tuple(&self) -> (u32, u32) {
(self.x.max(0) as u32, self.y.max(0) as u32)
}
}
impl Into<Vec2> for (i32,i32) {
fn into(self) -> Vec2 {
Vec2 { x: self.0, y: self.1 }
}
}
impl Into<Vec2> for (u32,u32) {
fn into(self) -> Vec2 {
Vec2 { x: self.0 as i32, y: self.1 as i32 }
}
}
impl Into<Vec2> for (f32,f32) {
fn into(self) -> Vec2 {
Vec2 {
x: self.0.floor() as i32,
y: self.1.floor() as i32,
}
}
}
impl std::ops::Add for Vec2 {
type Output = Self;
fn add(self, other: Self) -> Self {
Vec2 { x: self.x + other.x, y: self.y + other.y }
}
}
impl std::ops::Sub for Vec2 {
type Output = Self;
fn sub(self, other: Self) -> Self {
Vec2 { x: self.x - other.x, y: self.y - other.y }
}
}
pub struct Rect{
pub pos: Vec2,
pub size: Vec2,
}
impl Rect {
pub fn new(pos: Vec2, size: Vec2) -> Self {
Self {
pos: pos,
size: size,
}
}
pub fn contains(&self, point: Vec2) -> bool {
point.x >= self.pos.x &&
point.x < self.pos.x + self.size.x &&
point.y >= self.pos.y &&
point.y < self.pos.y + self.size.y
}
}