pub struct Vector<const N: usize> { /* private fields */ }Expand description
A vector with N plain f64 components.
§Examples
use use_vector::Vector;
let vector = Vector::<3>::from_array([2.0, 3.0, 6.0]);
assert_eq!(vector.dimension(), 3);
assert_eq!(vector.magnitude(), 7.0);Implementations§
Source§impl<const N: usize> Vector<N>
impl<const N: usize> Vector<N>
Sourcepub const fn from_array(components: [f64; N]) -> Vector<N>
pub const fn from_array(components: [f64; N]) -> Vector<N>
Creates a vector from its component array.
Sourcepub const fn into_array(self) -> [f64; N]
pub const fn into_array(self) -> [f64; N]
Returns the component array.
Sourcepub fn dot(self, other: Vector<N>) -> f64
pub fn dot(self, other: Vector<N>) -> f64
Returns the dot product with other.
§Examples
use use_vector::Vector;
let a = Vector::<3>::from_array([1.0, 2.0, 3.0]);
let b = Vector::<3>::from_array([4.0, 5.0, 6.0]);
assert_eq!(a.dot(b), 32.0);Sourcepub fn magnitude_squared(self) -> f64
pub fn magnitude_squared(self) -> f64
Returns the squared Euclidean magnitude.
Sourcepub fn normalize(self) -> Option<Vector<N>>
pub fn normalize(self) -> Option<Vector<N>>
Returns a normalized vector when the magnitude is finite and non-zero.
Returns None for zero vectors or vectors with non-finite magnitude.
§Examples
use use_vector::Vector2;
let unit = Vector2::new(3.0, 4.0)
.normalize()
.expect("non-zero finite vector should normalize");
assert!((unit.x() - 0.6).abs() < 1.0e-12);
assert!((unit.y() - 0.8).abs() < 1.0e-12);Sourcepub fn distance(self, other: Vector<N>) -> f64
pub fn distance(self, other: Vector<N>) -> f64
Returns the Euclidean distance to other.
§Examples
use use_vector::Vector2;
let start = Vector2::ZERO;
let end = Vector2::new(3.0, 4.0);
assert_eq!(start.distance(end), 5.0);Sourcepub fn distance_squared(self, other: Vector<N>) -> f64
pub fn distance_squared(self, other: Vector<N>) -> f64
Returns the squared Euclidean distance to other.
Sourcepub fn lerp(self, other: Vector<N>, t: f64) -> Vector<N>
pub fn lerp(self, other: Vector<N>, t: f64) -> Vector<N>
Returns the linear interpolation between self and other for t.
Sourcepub fn map_components(self, mapper: impl FnMut(f64) -> f64) -> Vector<N>
pub fn map_components(self, mapper: impl FnMut(f64) -> f64) -> Vector<N>
Maps each component with mapper.
Sourcepub fn zip_components(
self,
other: Vector<N>,
mapper: impl FnMut(f64, f64) -> f64,
) -> Vector<N>
pub fn zip_components( self, other: Vector<N>, mapper: impl FnMut(f64, f64) -> f64, ) -> Vector<N>
Combines components from self and other with mapper.
Sourcepub fn component_min(self, other: Vector<N>) -> Vector<N>
pub fn component_min(self, other: Vector<N>) -> Vector<N>
Returns the component-wise minimum with other.
Sourcepub fn component_max(self, other: Vector<N>) -> Vector<N>
pub fn component_max(self, other: Vector<N>) -> Vector<N>
Returns the component-wise maximum with other.
Sourcepub fn clamp_components(
self,
minimum: Vector<N>,
maximum: Vector<N>,
) -> Vector<N>
pub fn clamp_components( self, minimum: Vector<N>, maximum: Vector<N>, ) -> Vector<N>
Returns each component bounded by the matching minimum and maximum components.
Source§impl Vector<3>
impl Vector<3>
Trait Implementations§
impl<const N: usize> Copy for Vector<N>
impl<const N: usize> StructuralPartialEq for Vector<N>
Auto Trait Implementations§
impl<const N: usize> Freeze for Vector<N>
impl<const N: usize> RefUnwindSafe for Vector<N>
impl<const N: usize> Send for Vector<N>
impl<const N: usize> Sync for Vector<N>
impl<const N: usize> Unpin for Vector<N>
impl<const N: usize> UnsafeUnpin for Vector<N>
impl<const N: usize> UnwindSafe for Vector<N>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more