Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157use std::ops::{Add, Div, Mul, Rem, Sub};
pub type Point = Vec2<u16>;
impl Point { pub const ZERO: Self = Point::new(0, 0);
pub fn usize(self) -> Vec2<usize> { Vec2::new(self.x as usize, self.y as usize) }}
#[derive(Copy, Clone, Debug, PartialEq)]pub struct Vec2<T> { pub x: T, pub y: T,}
impl<T> Vec2<T> { pub const fn new(x: T, y: T) -> Self { Self { x, y } }}
impl<T: Copy> Vec2<T>where T: Add<Output = T>, T: Mul<Output = T>, T: Div<Output = T>, T: Rem<Output = T>,{ pub fn xy(idx: T, width: T) -> Self { Self { x: idx % width, y: idx / width, } }
pub fn idx(self, width: T) -> T { self.y * width + self.x }}
impl From<Point> for Vec2<usize> { fn from(p: Point) -> Self { Self::new(p.x as usize, p.y as usize) }}
impl<T: Add<Output = T>> Add for Vec2<T> { type Output = Vec2<T>;
fn add(self, rhs: Self) -> Self::Output { Self::new(self.x + rhs.x, self.y + rhs.y) }}
impl<T: Sub<Output = T>> Sub for Vec2<T> { type Output = Vec2<T>;
fn sub(self, rhs: Self) -> Self::Output { Self::new(self.x - rhs.x, self.y - rhs.y) }}
impl<T: Copy + Sub<Output = T>> Sub<T> for Vec2<T> { type Output = Vec2<T>;
fn sub(self, rhs: T) -> Self::Output { Self::new(self.x - rhs, self.y - rhs) }}
impl<T: Mul<Output = T>> Mul for Vec2<T> { type Output = Vec2<T>;
fn mul(self, rhs: Self) -> Self::Output { Self::new(self.x * rhs.x, self.y * rhs.y) }}
impl<T: Copy + Mul<Output = T>> Mul<T> for Vec2<T> { type Output = Vec2<T>;
fn mul(self, rhs: T) -> Self::Output { Self::new(self.x * rhs, self.y * rhs) }}
impl<T: Copy + Div<Output = T>> Div<T> for Vec2<T> { type Output = Vec2<T>;
fn div(self, rhs: T) -> Self::Output { Self::new(self.x / rhs, self.y / rhs) }}
#[inline(always)]pub fn greay32(k: u8) -> u32 { k as u32 | (k as u32) << 8 | (k as u32) << 16}
#[inline(always)]pub fn color32(r: u8, g: u8, b: u8) -> u32 { b as u32 | (g as u32) << 8 | (r as u32) << 16}
impl<T: AsSigned> Vec2<T> { pub fn signed(self) -> Vec2<T::Output> { Vec2::new(self.x.signed(), self.y.signed()) }}
impl<T: AsUnsigned> Vec2<T> { pub fn unsigned(self) -> Vec2<T::Output> { Vec2::new(self.x.unsigned(), self.y.unsigned()) }}
pub trait AsSigned { type Output;
fn signed(self) -> Self::Output;}
pub trait AsUnsigned { type Output;
fn unsigned(self) -> Self::Output;}
macro_rules! impl_num { (+$unsigned:ty, -$signed:ty) => { impl AsSigned for $unsigned { type Output = $signed;
fn signed(self) -> Self::Output { self.cast_signed() } }
impl AsUnsigned for $signed { type Output = $unsigned;
fn unsigned(self) -> Self::Output { self.cast_unsigned() } } };}
impl_num!(+u8, -i8);impl_num!(+u16, -i16);impl_num!(+u32, -i32);impl_num!(+u64, -i64);impl_num!(+usize, -isize);