//! Two-dimensional vectors and useful operations on them. use crate::math::Rect; use alga::general::{ClosedAdd, ClosedSub}; use nalgebra::Point2; use nalgebra::{RealField, Scalar}; use num_traits::{NumCast, One, ToPrimitive}; use serde::{Deserialize, Serialize}; use std::cmp::Ordering; use std::convert::{From, Into}; use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Sub, SubAssign}; use std::{fmt, mem}; /// Describes a vector, which may be a point or a directed length, depending on the interpretation. #[allow(missing_docs)] #[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize, Eq, Hash)] pub struct Vec2 { pub x: T, pub y: T, } impl Vec2 { /// Create a new vector from its internal values. pub fn new(x: T, y: T) -> Self { Self { x, y } } /// Finds the euclidian length and returns it. pub fn length(&self) -> T where T: RealField, { (self.x * self.x + self.y * self.y).sqrt() } /// Consumes the vector and returns a vector that is rotated by Pi/2 in clockwise direction. /// This is a special case of rotation and the function is faster than using the nonspecific /// rotation function. pub fn rotated_90_clockwise(mut self) -> Vec2 where T: One + Neg + MulAssign, { mem::swap(&mut self.x, &mut self.y); self.y *= -T::one(); self } /// Consumes the vector and returns a vector that is rotated by Pi/2 in counterclockwise direction. /// This is a special case of rotation and the function is faster than using the nonspecific /// rotation function. pub fn rotated_90_counterclockwise(mut self) -> Vec2 where T: One + Neg + MulAssign, { mem::swap(&mut self.x, &mut self.y); self.x *= -T::one(); self } } impl Into> for Vec2 { fn into(self) -> Point2 { Point2::new(self.x, self.y) } } impl From> for Vec2 { fn from(v: Point2) -> Self { Self::new(v.x, v.y) } } // This is sad, but also sadly necessary :/ impl From for Vec2 where T: From + Scalar + Copy, { fn from(v: raylib::ffi::Vector2) -> Self { Self { x: T::from(v.x), y: T::from(v.y), } } } impl From for Vec2 where T: From + Scalar + Copy, { fn from(v: raylib::math::Vector2) -> Self { Self { x: T::from(v.x), y: T::from(v.y), } } } impl Into for Vec2 { fn into(self) -> raylib::ffi::Vector2 { raylib::ffi::Vector2 { x: NumCast::from(self.x).expect("Unable to cast Vec2 into raylib Vector"), y: NumCast::from(self.y).expect("Unable to cast Vec2 into raylib Vector"), } } } impl Into for Vec2 { fn into(self) -> raylib::math::Vector2 { raylib::math::Vector2 { x: NumCast::from(self.x).expect("Unable to cast Vec2 into raylib Vector"), y: NumCast::from(self.y).expect("Unable to cast Vec2 into raylib Vector"), } } } // Begin mathematical operators ----------------------------------------------- // Addition impl Add for Vec2 { type Output = Self; fn add(self, rhs: Self) -> Self { Vec2::new(self.x + rhs.x, self.y + rhs.y) } } impl Add<(T, T)> for Vec2 { type Output = Self; fn add(self, (x, y): (T, T)) -> Self { Vec2::new(self.x + x, self.y + y) } } impl Add for Vec2 { type Output = Self; fn add(self, rhs: T) -> Self { Vec2::new(self.x + rhs, self.y + rhs) } } impl AddAssign for Vec2 { fn add_assign(&mut self, rhs: Self) { self.x += rhs.x; self.y += rhs.y; } } impl AddAssign<(T, T)> for Vec2 { fn add_assign(&mut self, (x, y): (T, T)) { self.x += x; self.y += y; } } // Subtraction impl Sub for Vec2 { type Output = Self; fn sub(self, rhs: Self) -> Self { Vec2::new(self.x - rhs.x, self.y - rhs.y) } } impl Sub<(T, T)> for Vec2 { type Output = Self; fn sub(self, (x, y): (T, T)) -> Self { Vec2::new(self.x - x, self.y - y) } } impl Sub for Vec2 { type Output = Self; fn sub(self, rhs: T) -> Self { Vec2::new(self.x - rhs, self.y - rhs) } } impl SubAssign for Vec2 { fn sub_assign(&mut self, rhs: Self) { self.x -= rhs.x; self.y -= rhs.y; } } impl SubAssign<(T, T)> for Vec2 { fn sub_assign(&mut self, (x, y): (T, T)) { self.x -= x; self.y -= y; } } // Scalar multiplication impl + Mul + Copy> Mul for Vec2 { type Output = T; fn mul(self, rhs: Self) -> T { self.x * rhs.x + self.y * rhs.y } } impl + Copy> Mul for Vec2 { type Output = Self; fn mul(self, rhs: T) -> Self { Vec2::new(self.x * rhs, self.y * rhs) } } impl + Copy> Div for Vec2 { type Output = Self; fn div(self, rhs: T) -> Self { Vec2::new(self.x / rhs, self.y / rhs) } } // End of mathematical operators ---------------------------------------------- // By default, the coordinates are first compared by their y-coordinates, then // their x-coordinates impl PartialOrd for Vec2 where T: PartialOrd + Copy + 'static, { fn partial_cmp(&self, other: &Self) -> Option { match self.y.partial_cmp(&other.y) { Some(Ordering::Equal) | None => self.x.partial_cmp(&other.x), y_order => y_order, } } } impl Ord for Vec2 where T: Ord + Copy + 'static, { fn cmp(&self, other: &Self) -> Ordering { match self.y.cmp(&other.y) { Ordering::Equal => self.x.cmp(&other.x), y_order => y_order, } } } // Helper function to determine the absolute positive difference between two // Values, which don't have to be signed. fn difference_abs(a: T, b: T) -> T where T: ClosedSub + PartialOrd, { if a > b { a - b } else { b - a } } // Helper function that removes all points inside the vector that are not // contained inside the optional limit Rect fn retain_inside_limits(items: Vec>, limits: Option>) -> Vec> where T: PartialOrd + std::fmt::Debug + Copy + Add, { // Fast return in case there are no limits if limits.is_none() { return items; } let limits = limits.unwrap(); // Retain only items that are within the bounds of the limits rect items .into_iter() .filter(|v| { v.x >= limits.x && v.x <= limits.x + limits.w && v.y >= limits.y && v.y <= limits.y + limits.h }) .collect() }