aboutsummaryrefslogtreecommitdiff
path: root/src/math/vec2.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/math/vec2.rs')
-rw-r--r--src/math/vec2.rs14
1 files changed, 13 insertions, 1 deletions
diff --git a/src/math/vec2.rs b/src/math/vec2.rs
index b9e3215..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};
@@ -7,8 +10,9 @@ use std::cmp::Ordering;
use std::convert::{From, Into};
use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Sub, SubAssign};
use std::{fmt, mem};
-use nalgebra::Point2;
+/// 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,
@@ -16,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,
@@ -27,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,
@@ -36,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,