Skip to main content

Vec4

Struct Vec4 

Source
pub struct Vec4<T> {
    pub x: T,
    pub y: T,
    pub z: T,
    pub w: T,
}
Expand description

Represents a 4D vector with generic numeric components.

Vec4 is a generic structure that represents a 4-dimensional vector with components x, y, z, and w. It provides methods for vector operations such as vector arithmetic, normalization, dot product, and linear interpolation.

§Type Parameters

  • T: The numeric type of the vector components. This type must implement traits like Zero, One, NumAssign, Copy, and others depending on the method.

Fields§

§x: T§y: T§z: T§w: T

Implementations§

Source§

impl<T> Vec4<T>
where T: NumAssign + Copy,

Source

pub fn new(x: T, y: T, z: T, w: T) -> Self

Creates a new Vec4 with the specified components.

§Parameters
  • x: The x component of the vector.
  • y: The y component of the vector.
  • z: The z component of the vector.
  • w: The w component of the vector.
§Returns

Returns a new Vec4 instance with the specified components.

§Examples
let vec = Vec4::new(1.0, 2.0, 3.0, 4.0);
assert_eq!(vec, Vec4::new(1.0, 2.0, 3.0, 4.0)); // Vector with components (1, 2, 3, 4)
Source

pub fn zero() -> Self

Creates a new Vec4 with all components set to zero.

§Returns

Returns a new Vec4 instance where all components are zero.

§Examples
let vec = Vec4::zero();
assert_eq!(vec, Vec4::new(0.0, 0.0, 0.0, 0.0)); // Vector with all components zero
Source

pub fn one() -> Self

Creates a new Vec4 with all components set to one.

§Returns

Returns a new Vec4 instance where all components are one.

§Examples
let vec = Vec4::one();
assert_eq!(vec, Vec4::new(1.0, 1.0, 1.0, 1.0)); // Vector with all components one
Source

pub fn set(v: T) -> Self

Creates a new Vec4 where all components are set to the same value.

§Parameters
  • v: The value to set for all components.
§Returns

Returns a new Vec4 instance where all components are set to v.

§Examples
let vec = Vec4::set(5.0);
assert_eq!(vec, Vec4::new(5.0, 5.0, 5.0, 5.0)); // Vector with all components 5
Source

pub fn from_vec2(v: &Vec2<T>) -> Self

Creates a Vec4 from a Vec2 by setting the z and w components to zero.

§Parameters
  • v: The Vec2 to convert.
§Returns

Returns a new Vec4 instance where the x and y components are taken from v, and the z and w components are set to zero.

§Examples
let vec2 = Vec2::new(1.0, 2.0);
let vec4 = Vec4::from_vec2(&vec2);
assert_eq!(vec4, Vec4::new(1.0, 2.0, 0.0, 0.0)); // Vector (1, 2, 0, 0)
Source

pub fn from_vec3(v: &Vec3<T>) -> Self

Creates a Vec4 from a Vec3 by setting the w component to zero.

§Parameters
  • v: The Vec3 to convert.
§Returns

Returns a new Vec4 instance where the x, y, and z components are taken from v, and the w component is set to zero.

§Examples
let vec3 = Vec3::new(1.0, 2.0, 3.0);
let vec4 = Vec4::from_vec3(&vec3);
assert_eq!(vec4, Vec4::new(1.0, 2.0, 3.0, 0.0)); // Vector (1, 2, 3, 0)
Source

pub fn dot(&self, other: &Self) -> T

Computes the dot product of this vector and another vector.

§Parameters
  • other: The vector to compute the dot product with.
§Returns

Returns the dot product of self and other as a value of type T.

§Examples
let vec1 = Vec4::new(1.0, 2.0, 3.0, 4.0);
let vec2 = Vec4::new(5.0, 6.0, 7.0, 8.0);
let dot_product = vec1.dot(&vec2);
assert_eq!(dot_product, 70.0); // Dot product calculation
Source

pub fn length_squared(&self) -> T

Computes the squared length (magnitude) of this vector.

§Returns

Returns the squared length of the vector as a value of type T.

§Examples
let vec = Vec4::new(1.0, 2.0, 3.0, 4.0);
let length_squared = vec.length_squared();
assert_eq!(length_squared, 30.0); // Squared length calculation
Source

pub fn transform(&self, transform: &Mat4<T>) -> Self

Applies a transformation matrix to this vector.

The vector is transformed using a 4x4 matrix.

§Parameters
  • transform: The transformation matrix to apply.
§Returns

Returns a new Vec4 instance that is the result of transforming self using transform.

§Examples
let vec = Vec4::new(1.0, 2.0, 3.0, 1.0);
let transform = Mat4::identity(); // Assuming an identity matrix
let transformed_vec = vec.transform(&transform);
assert_eq!(transformed_vec, Vec4::new(1.0, 2.0, 3.0, 1.0)); // No change for identity matrix
Source§

impl<T> Vec4<T>
where T: NumAssign + Copy + PartialOrd,

Source

pub fn min(&self, other: &Self) -> Self

Computes the component-wise minimum of this vector and another vector.

For each component, this method returns the minimum of the corresponding components of self and other.

§Parameters
  • other: The vector to compare against.
§Returns

Returns a new Vec4 instance where each component is the minimum of the corresponding components of self and other.

§Examples
let vec1 = Vec4::new(1.0, 2.0, 3.0, 4.0);
let vec2 = Vec4::new(2.0, 1.0, 4.0, 3.0);
let min_vec = vec1.min(&vec2);
assert_eq!(min_vec, Vec4::new(1.0, 1.0, 3.0, 3.0)); // Minimum component-wise
Source

pub fn max(&self, other: &Self) -> Self

Computes the component-wise maximum of this vector and another vector.

For each component, this method returns the maximum of the corresponding components of self and other.

§Parameters
  • other: The vector to compare against.
§Returns

Returns a new Vec4 instance where each component is the maximum of the corresponding components of self and other.

§Examples
let vec1 = Vec4::new(1.0, 2.0, 3.0, 4.0);
let vec2 = Vec4::new(2.0, 1.0, 4.0, 3.0);
let max_vec = vec1.max(&vec2);
assert_eq!(max_vec, Vec4::new(2.0, 2.0, 4.0, 4.0)); // Maximum component-wise
Source

pub fn clamp(&self, min: &Self, max: &Self) -> Self

Clamps each component of this vector between the corresponding components of the given min and max vectors.

For each component, this method ensures that the component of self is at least the corresponding component of min and at most the corresponding component of max.

§Parameters
  • min: The vector representing the minimum bounds.
  • max: The vector representing the maximum bounds.
§Returns

Returns a new Vec4 instance where each component is clamped between the corresponding components of min and max.

§Examples
let vec = Vec4::new(5.0, -3.0, 10.0, 2.0);
let min_vec = Vec4::new(0.0, 0.0, 5.0, 1.0);
let max_vec = Vec4::new(4.0, 2.0, 8.0, 3.0);
let clamped_vec = vec.clamp(&min_vec, &max_vec);
assert_eq!(clamped_vec, Vec4::new(4.0, 0.0, 8.0, 2.0)); // Clamped component-wise
Source§

impl<T> Vec4<T>
where T: NumAssign + Float,

Source

pub fn length(&self) -> T

Computes the length (magnitude) of this Vec4.

This method calculates the Euclidean length of the vector using the formula: sqrt(x^2 + y^2 + z^2 + w^2).

§Returns

Returns the length of the vector as a T.

§Examples
let vec = Vec4::new(3.0, 4.0, 0.0, 0.0);
let length = vec.length();
assert_eq!(length, 5.0); // Euclidean length of the vector (3, 4, 0, 0)
Source

pub fn normalize(&self) -> Option<Self>

Normalizes the vector, scaling it to a unit vector.

This method returns a new Vec4 with the same direction as self but with a length of 1. If the vector length is zero, meaning it’s a zero vector, normalization is not possible, and None is returned to indicate this failure.

§Returns
  • Some(Self): A unit vector in the same direction as self if normalization is possible.
  • None: If self is a zero vector (length is zero), normalization is not possible.
§Examples
let vec = Vec4::new(1.0, 2.0, 2.0, 0.0);
let normalized = vec.normalize();
assert_eq!(normalized, Some(Vec4::new(0.33333334, 0.6666667, 0.6666667, 0.0)));
Source

pub fn lerp(&self, other: &Self, t: T) -> Self

Performs linear interpolation between this vector and another vector.

This method returns a vector that is a linear interpolation between self and other by a factor t. If t is 0, the result is self. If t is 1, the result is other. Values of t between 0 and 1 will yield a result that is a blend of the two vectors.

§Parameters
  • other: The target vector to interpolate towards.
  • t: The interpolation factor, where t is between 0 and 1.
§Returns

Returns a new Vec4 that is the result of the interpolation.

§Examples
let vec1 = Vec4::new(1.0, 2.0, 3.0, 4.0);
let vec2 = Vec4::new(5.0, 6.0, 7.0, 8.0);
let t = 0.5;
let interpolated = vec1.lerp(&vec2, t);
assert_eq!(interpolated, Vec4::new(3.0, 4.0, 5.0, 6.0)); // Midpoint between vec1 and vec2

Trait Implementations§

Source§

impl<T> Add for Vec4<T>
where T: NumAssign + Copy,

Source§

type Output = Vec4<T>

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Self

Performs the + operation. Read more
Source§

impl<T> Add<T> for Vec4<T>
where T: NumAssign + Copy,

Source§

type Output = Vec4<T>

The resulting type after applying the + operator.
Source§

fn add(self, scalar: T) -> Self

Performs the + operation. Read more
Source§

impl<T> AddAssign for Vec4<T>
where T: NumAssign + Copy,

Source§

fn add_assign(&mut self, other: Self)

Performs the += operation. Read more
Source§

impl<T> AddAssign<T> for Vec4<T>
where T: NumAssign + Copy,

Source§

fn add_assign(&mut self, scalar: T)

Performs the += operation. Read more
Source§

impl<T: Clone> Clone for Vec4<T>

Source§

fn clone(&self) -> Vec4<T>

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<T: Copy> Copy for Vec4<T>

Source§

impl<T: Debug> Debug for Vec4<T>

Source§

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

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

impl<T: Default> Default for Vec4<T>

Source§

fn default() -> Vec4<T>

Returns the “default value” for a type. Read more
Source§

impl<T> Display for Vec4<T>
where T: Display,

Source§

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

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

impl<T> Div for Vec4<T>
where T: NumAssign + Copy,

Source§

type Output = Vec4<T>

The resulting type after applying the / operator.
Source§

fn div(self, other: Self) -> Self

Performs the / operation. Read more
Source§

impl<T> Div<T> for Vec4<T>
where T: NumAssign + Copy,

Source§

type Output = Vec4<T>

The resulting type after applying the / operator.
Source§

fn div(self, scalar: T) -> Self

Performs the / operation. Read more
Source§

impl<T> DivAssign for Vec4<T>
where T: NumAssign + Copy,

Source§

fn div_assign(&mut self, other: Self)

Performs the /= operation. Read more
Source§

impl<T> DivAssign<T> for Vec4<T>
where T: NumAssign + Copy,

Source§

fn div_assign(&mut self, scalar: T)

Performs the /= operation. Read more
Source§

impl<T> From<(T, T, T, T)> for Vec4<T>
where T: NumAssign + Copy,

Source§

fn from(tuple: (T, T, T, T)) -> Self

Converts to this type from the input type.
Source§

impl<T> Index<usize> for Vec4<T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

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

impl<T> IndexMut<usize> for Vec4<T>

Source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

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

impl<T> Into<(T, T, T, T)> for Vec4<T>
where T: NumAssign + Copy,

Source§

fn into(self) -> (T, T, T, T)

Converts this type into the (usually inferred) input type.
Source§

impl<'a, T> IntoIterator for &'a Vec4<T>
where T: NumAssign + Copy,

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T> IntoIterator for &'a mut Vec4<T>
where T: NumAssign + Copy,

Source§

type Item = &'a mut T

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T> Mul for Vec4<T>
where T: NumAssign + Copy,

Source§

type Output = Vec4<T>

The resulting type after applying the * operator.
Source§

fn mul(self, other: Self) -> Self

Performs the * operation. Read more
Source§

impl<T> Mul<T> for Vec4<T>
where T: NumAssign + Copy,

Source§

type Output = Vec4<T>

The resulting type after applying the * operator.
Source§

fn mul(self, scalar: T) -> Self

Performs the * operation. Read more
Source§

impl<T> MulAssign for Vec4<T>
where T: NumAssign + Copy,

Source§

fn mul_assign(&mut self, other: Self)

Performs the *= operation. Read more
Source§

impl<T> MulAssign<T> for Vec4<T>
where T: NumAssign + Copy,

Source§

fn mul_assign(&mut self, scalar: T)

Performs the *= operation. Read more
Source§

impl<T> Neg for Vec4<T>
where T: NumAssign + Copy + Neg<Output = T>,

Source§

type Output = Vec4<T>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl<T: PartialEq> PartialEq for Vec4<T>

Source§

fn eq(&self, other: &Vec4<T>) -> 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<T> StructuralPartialEq for Vec4<T>

Source§

impl<T> Sub for Vec4<T>
where T: NumAssign + Copy,

Source§

type Output = Vec4<T>

The resulting type after applying the - operator.
Source§

fn sub(self, other: Self) -> Self

Performs the - operation. Read more
Source§

impl<T> Sub<T> for Vec4<T>
where T: NumAssign + Copy,

Source§

type Output = Vec4<T>

The resulting type after applying the - operator.
Source§

fn sub(self, scalar: T) -> Self

Performs the - operation. Read more
Source§

impl<T> SubAssign for Vec4<T>
where T: NumAssign + Copy,

Source§

fn sub_assign(&mut self, other: Self)

Performs the -= operation. Read more
Source§

impl<T> SubAssign<T> for Vec4<T>
where T: NumAssign + Copy,

Source§

fn sub_assign(&mut self, scalar: T)

Performs the -= operation. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Vec4<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Vec4<T>
where T: RefUnwindSafe,

§

impl<T> Send for Vec4<T>
where T: Send,

§

impl<T> Sync for Vec4<T>
where T: Sync,

§

impl<T> Unpin for Vec4<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Vec4<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for Vec4<T>
where T: UnwindSafe,

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.