pub struct Vector<T, const DIM: usize> { /* private fields */ }Implementations§
Source§impl<T, const DIM: usize> Vector<T, DIM>
impl<T, const DIM: usize> Vector<T, DIM>
Sourcepub fn new(value: impl Into<Vector<T, DIM>>) -> Self
pub fn new(value: impl Into<Vector<T, DIM>>) -> Self
Create a new Vector
let v = Vector::new([1, 2]);pub fn repeat(value: T) -> Selfwhere
T: Clone,
Sourcepub fn try_new(i: impl IntoIterator<Item = T>) -> Option<Self>
pub fn try_new(i: impl IntoIterator<Item = T>) -> Option<Self>
Try to create a vector from the elements provided (in the form of any
type which implements IntoIterator). Returns None when the number of
items in the iterator do no much the dimension of the desired vector.
assert_eq!(Vector::try_new(vec![1, 2]), Some(Vector::new2(1, 2)));
assert_eq!(Vector::try_new(vec![1, 2, 3]), Option::<Vector<_, 2>>::None);
assert_eq!(Vector::try_new(vec![1]), Option::<Vector<_, 2>>::None);Sourcepub fn try_new_overflow(i: impl IntoIterator<Item = T>) -> Option<Self>
pub fn try_new_overflow(i: impl IntoIterator<Item = T>) -> Option<Self>
Like [try_new], but the iterator provided may be longer than the desired
vector (extra elements are consumed).
However, it may not be shorter then the desired vector.
assert_eq!(Vector::try_new_overflow(vec![1, 2]), Some(Vector::new2(1, 2)));
assert_eq!(Vector::try_new_overflow(vec![1, 2, 3]), Some(Vector::new2(1, 2)));
assert_eq!(Vector::try_new_overflow(vec![1, 2, 3]), Some(Vector::new3(1, 2, 3)));
assert_eq!(Vector::try_new_overflow(vec![1]), Option::<Vector<_, 2>>::None);Sourcepub fn dimensions(&self) -> usize
pub fn dimensions(&self) -> usize
Get the number of dimensions this vector has
let v = Vector::new([1, 2, 3, 4]);
assert_eq!(v.dimensions(), 4);Sourcepub fn get(&self, n: usize) -> Option<&T>
pub fn get(&self, n: usize) -> Option<&T>
get a reference to the nth item in the vector
let mut v = Vector::new2(1, 2);
assert_eq!(v.get(0), Some(&1));
assert_eq!(v.get(1), Some(&2));
assert_eq!(v.get(2), None);Sourcepub fn get_mut(&mut self, n: usize) -> Option<&mut T>
pub fn get_mut(&mut self, n: usize) -> Option<&mut T>
get a mutable reference to the nth item in the vector
let mut v = Vector::new2(1, 2);
assert_eq!(v.get_mut(0), Some(&mut 1));
assert_eq!(v.get_mut(1), Some(&mut 2));
assert_eq!(v.get_mut(2), None);Sourcepub fn iter(&self) -> impl Iterator<Item = &T>
pub fn iter(&self) -> impl Iterator<Item = &T>
Create an iterator over references to items in the vector
let v = Vector::new2(1, 2);
let mut i = v.iter();
assert_eq!(i.next(), Some(&1));
assert_eq!(i.next(), Some(&2));
assert_eq!(i.next(), None);Sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T>
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T>
Create an iterator over mutable references to items in the vector
let mut v = Vector::new2(1, 2);
let mut i = v.iter_mut();
assert_eq!(i.next(), Some(&mut 1));
assert_eq!(i.next(), Some(&mut 2));
assert_eq!(i.next(), None);
let mut v = Vector::new2(1, 2);
{
let mut i = v.iter_mut();
*i.next().unwrap() = 4;
}
assert_eq!(v.get(0), Some(&4))Sourcepub fn scale<'a, U>(&'a self, n: U) -> Vector<<&'a T as Mul<U>>::Output, DIM>
pub fn scale<'a, U>(&'a self, n: U) -> Vector<<&'a T as Mul<U>>::Output, DIM>
Scale a vector by a scalar, multiplying each element by n.
let v = Vector::new([1, 2, 3]);
assert_eq!(v.scale(2), Vector::new([2, 4, 6]));
assert_eq!(v.scale(3), Vector::new([3, 6, 9]));
Sourcepub fn unscale<'a, U>(
&'a self,
other: U,
) -> Vector<<&'a T as Div<U>>::Output, DIM>
pub fn unscale<'a, U>( &'a self, other: U, ) -> Vector<<&'a T as Div<U>>::Output, DIM>
Unscale a vector by a scalar. This divides every element by n.
let v = Vector::new([4, 8, 16]);
assert_eq!(v.unscale(2), Vector::new([2, 4, 8]));
assert_eq!(v.unscale(4), Vector::new([1, 2, 4]));
Source§impl<T, const DIM: usize> Vector<T, DIM>
impl<T, const DIM: usize> Vector<T, DIM>
Sourcepub fn magnitude(&self) -> f64where
T: Clone,
pub fn magnitude(&self) -> f64where
T: Clone,
Calculate the magnitude of this vector
let mut v = Vector::new2(3, 4);
assert_eq!(v.magnitude(), 5.0)Sourcepub fn with_magnitude(&self, magnitude: impl Into<f64>) -> Vector<f64, DIM>
pub fn with_magnitude(&self, magnitude: impl Into<f64>) -> Vector<f64, DIM>
Create a new vector with the same direction but another magnitude
let mut v = Vector::new2(3, 4);
assert_eq!(v.with_magnitude(10), Vector::new((6.0, 8.0)))Sourcepub fn normalize(&self) -> Vector<f64, DIM>
pub fn normalize(&self) -> Vector<f64, DIM>
Normalizes the vector. Sets the magnitude to 1.
let mut v = Vector::new2(3, 4);
assert_eq!(v.normalize(), Vector::new((3.0/5.0, 4.0/5.0)))Sourcepub fn limit(&self, limit: impl Into<f64>) -> Vector<f64, DIM>
pub fn limit(&self, limit: impl Into<f64>) -> Vector<f64, DIM>
Limit the magnitude of a vector. If the magnitude is less than the limit nothing changes (except all values are cast to floats). If the magnitude is larger than the limit, the magnitude is set to this limit.
assert_eq!(Vector::new2(3, 4).limit(10), Vector::new((3.0, 4.0)));
assert_eq!(Vector::new2(9, 12).limit(10), Vector::new((6.0, 8.0)));Sourcepub fn angle<O>(&self, other: &Vector<O, DIM>) -> f64
pub fn angle<O>(&self, other: &Vector<O, DIM>) -> f64
Calculates the angle between two vectors (in radians)
let mut v1 = Vector::new2(0, 1);
let mut v2 = Vector::new2(1, 0);
assert_eq!(v1.angle(&v2).to_degrees(), 90.0)Sourcepub fn distance<O>(&self, other: &Vector<O, DIM>) -> f64
pub fn distance<O>(&self, other: &Vector<O, DIM>) -> f64
Calculate the distance from this vector to another vector
let mut v1 = Vector::new2(0, 0);
let mut v2 = Vector::new2(3, 4);
assert_eq!(v1.distance(&v2), 5.0)Sourcepub fn dot<O>(&self, other: &Vector<O, DIM>) -> <T as Mul<O>>::Output
pub fn dot<O>(&self, other: &Vector<O, DIM>) -> <T as Mul<O>>::Output
Calculate the dot product of this vector
let v1 = Vector::new((1, 2));
let v2 = Vector::new((&2, &1));
assert_eq!(v1.dot(&v2), 4)
Sourcepub fn perpendicular<O>(&self, other: &Vector<O, DIM>) -> bool
pub fn perpendicular<O>(&self, other: &Vector<O, DIM>) -> bool
Find if the angle between two vectors is 90 degrees
let v1 = Vector::new((0, 1));
let v2 = Vector::new((1, 0));
let v3 = Vector::new((1, 1));
assert!(v1.perpendicular(&v2));
assert!(!v1.perpendicular(&v3));