Skip to main content

Vector

Struct Vector 

Source
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>

Source

pub const ZERO: Vector<N>

The zero vector.

Source

pub const ONE: Vector<N>

A vector with every component set to 1.0.

Source

pub const fn from_array(components: [f64; N]) -> Vector<N>

Creates a vector from its component array.

Source

pub const fn as_array(&self) -> &[f64; N]

Returns a shared reference to the component array.

Source

pub const fn into_array(self) -> [f64; N]

Returns the component array.

Source

pub const fn dimension(self) -> usize

Returns the vector dimension.

Source

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);
Source

pub fn magnitude_squared(self) -> f64

Returns the squared Euclidean magnitude.

Source

pub fn magnitude(self) -> f64

Returns the Euclidean magnitude.

Source

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);
Source

pub fn scale(self, scalar: f64) -> Vector<N>

Returns the vector scaled by scalar.

Source

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);
Source

pub fn distance_squared(self, other: Vector<N>) -> f64

Returns the squared Euclidean distance to other.

Source

pub fn lerp(self, other: Vector<N>, t: f64) -> Vector<N>

Returns the linear interpolation between self and other for t.

Source

pub fn map_components(self, mapper: impl FnMut(f64) -> f64) -> Vector<N>

Maps each component with mapper.

Source

pub fn zip_components( self, other: Vector<N>, mapper: impl FnMut(f64, f64) -> f64, ) -> Vector<N>

Combines components from self and other with mapper.

Source

pub fn component_min(self, other: Vector<N>) -> Vector<N>

Returns the component-wise minimum with other.

Source

pub fn component_max(self, other: Vector<N>) -> Vector<N>

Returns the component-wise maximum with other.

Source

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

pub fn abs(self) -> Vector<N>

Returns a vector with the absolute value of each component.

Source

pub fn is_finite(self) -> bool

Returns true when every component is finite.

Source

pub fn is_nan(self) -> bool

Returns true when any component is NaN.

Source§

impl Vector<2>

Source

pub const fn new(x: f64, y: f64) -> Vector<2>

Creates a two-dimensional vector from its components.

Source

pub const fn x(self) -> f64

Returns the x component.

Source

pub const fn y(self) -> f64

Returns the y component.

Source§

impl Vector<3>

Source

pub const fn new(x: f64, y: f64, z: f64) -> Vector<3>

Creates a three-dimensional vector from its components.

Source

pub const fn x(self) -> f64

Returns the x component.

Source

pub const fn y(self) -> f64

Returns the y component.

Source

pub const fn z(self) -> f64

Returns the z component.

Source

pub fn cross(self, other: Vector<3>) -> Vector<3>

Returns the cross product with other.

§Examples
use use_vector::Vector3;

let x = Vector3::new(1.0, 0.0, 0.0);
let y = Vector3::new(0.0, 1.0, 0.0);

assert_eq!(x.cross(y), Vector3::new(0.0, 0.0, 1.0));
Source§

impl Vector<4>

Source

pub const fn new(x: f64, y: f64, z: f64, w: f64) -> Vector<4>

Creates a four-dimensional vector from its components.

Source

pub const fn x(self) -> f64

Returns the x component.

Source

pub const fn y(self) -> f64

Returns the y component.

Source

pub const fn z(self) -> f64

Returns the z component.

Source

pub const fn w(self) -> f64

Returns the w component.

Trait Implementations§

Source§

impl<const N: usize> Add for Vector<N>

Source§

type Output = Vector<N>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Vector<N>) -> <Vector<N> as Add>::Output

Performs the + operation. Read more
Source§

impl<const N: usize> AsRef<[f64; N]> for Vector<N>

Source§

fn as_ref(&self) -> &[f64; N]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<const N: usize> Clone for Vector<N>

Source§

fn clone(&self) -> Vector<N>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const N: usize> Debug for Vector<N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<const N: usize> Div<f64> for Vector<N>

Source§

type Output = Vector<N>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: f64) -> <Vector<N> as Div<f64>>::Output

Performs the / operation. Read more
Source§

impl<const N: usize> From<[f64; N]> for Vector<N>

Source§

fn from(components: [f64; N]) -> Vector<N>

Converts to this type from the input type.
Source§

impl<const N: usize> Index<usize> for Vector<N>

Source§

type Output = f64

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &<Vector<N> as Index<usize>>::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<const N: usize> IndexMut<usize> for Vector<N>

Source§

fn index_mut( &mut self, index: usize, ) -> &mut <Vector<N> as Index<usize>>::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl Mul<Vector<2>> for Matrix2

Source§

type Output = Vector<2>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Vector<2>) -> <Matrix2 as Mul<Vector<2>>>::Output

Performs the * operation. Read more
Source§

impl Mul<Vector<3>> for Matrix3

Source§

type Output = Vector<3>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Vector<3>) -> <Matrix3 as Mul<Vector<3>>>::Output

Performs the * operation. Read more
Source§

impl Mul<Vector<4>> for Matrix4

Source§

type Output = Vector<4>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Vector<4>) -> <Matrix4 as Mul<Vector<4>>>::Output

Performs the * operation. Read more
Source§

impl<const N: usize> Mul<f64> for Vector<N>

Source§

type Output = Vector<N>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: f64) -> <Vector<N> as Mul<f64>>::Output

Performs the * operation. Read more
Source§

impl<const N: usize> Neg for Vector<N>

Source§

type Output = Vector<N>

The resulting type after applying the - operator.
Source§

fn neg(self) -> <Vector<N> as Neg>::Output

Performs the unary - operation. Read more
Source§

impl<const N: usize> PartialEq for Vector<N>

Source§

fn eq(&self, other: &Vector<N>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<const N: usize> Sub for Vector<N>

Source§

type Output = Vector<N>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Vector<N>) -> <Vector<N> as Sub>::Output

Performs the - operation. Read more
Source§

impl<const N: usize> Copy for Vector<N>

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.