diff options
Diffstat (limited to 'src/math/vec2.rs')
| -rw-r--r-- | src/math/vec2.rs | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/math/vec2.rs b/src/math/vec2.rs index d591f1d..b5706a0 100644 --- a/src/math/vec2.rs +++ b/src/math/vec2.rs @@ -1,5 +1,8 @@ +//! 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}; @@ -8,6 +11,8 @@ 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<T: Scalar + Copy> { pub x: T, @@ -15,10 +20,12 @@ pub struct Vec2<T: Scalar + Copy> { } impl<T: Scalar + Copy> Vec2<T> { + /// 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, @@ -26,6 +33,9 @@ impl<T: Scalar + Copy> Vec2<T> { (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<T> where T: One + Neg<Output = T> + MulAssign, @@ -35,6 +45,9 @@ impl<T: Scalar + Copy> Vec2<T> { 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<T> where T: One + Neg<Output = T> + MulAssign, @@ -45,6 +58,18 @@ impl<T: Scalar + Copy> Vec2<T> { } } +impl<T: Scalar + Copy> Into<Point2<T>> for Vec2<T> { + fn into(self) -> Point2<T> { + Point2::new(self.x, self.y) + } +} + +impl<T: Scalar + Copy> From<Point2<T>> for Vec2<T> { + fn from(v: Point2<T>) -> Self { + Self::new(v.x, v.y) + } +} + // This is sad, but also sadly necessary :/ impl<T> From<raylib::ffi::Vector2> for Vec2<T> where |
