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 likeZero,One,NumAssign,Copy, and others depending on the method.
Fields§
§x: T§y: T§z: T§w: TImplementations§
Source§impl<T> Vec4<T>
impl<T> Vec4<T>
Sourcepub fn new(x: T, y: T, z: T, w: T) -> Self
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)Sourcepub fn set(v: T) -> Self
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 5Sourcepub fn from_vec2(v: &Vec2<T>) -> Self
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: TheVec2to 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)Sourcepub fn from_vec3(v: &Vec3<T>) -> Self
pub fn from_vec3(v: &Vec3<T>) -> Self
Creates a Vec4 from a Vec3 by setting the w component to zero.
§Parameters
v: TheVec3to 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)Sourcepub fn dot(&self, other: &Self) -> T
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 calculationSourcepub fn length_squared(&self) -> T
pub fn length_squared(&self) -> T
Sourcepub fn transform(&self, transform: &Mat4<T>) -> Self
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 matrixSource§impl<T> Vec4<T>
impl<T> Vec4<T>
Sourcepub fn min(&self, other: &Self) -> Self
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-wiseSourcepub fn max(&self, other: &Self) -> Self
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-wiseSourcepub fn clamp(&self, min: &Self, max: &Self) -> Self
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-wiseSource§impl<T> Vec4<T>
impl<T> Vec4<T>
Sourcepub fn length(&self) -> T
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)Sourcepub fn normalize(&self) -> Option<Self>
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 asselfif normalization is possible.None: Ifselfis 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)));Sourcepub fn lerp(&self, other: &Self, t: T) -> Self
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, wheretis 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 vec2Trait Implementations§
Source§impl<T> AddAssign for Vec4<T>
impl<T> AddAssign for Vec4<T>
Source§fn add_assign(&mut self, other: Self)
fn add_assign(&mut self, other: Self)
+= operation. Read moreSource§impl<T> AddAssign<T> for Vec4<T>
impl<T> AddAssign<T> for Vec4<T>
Source§fn add_assign(&mut self, scalar: T)
fn add_assign(&mut self, scalar: T)
+= operation. Read moreimpl<T: Copy> Copy for Vec4<T>
Source§impl<T> DivAssign for Vec4<T>
impl<T> DivAssign for Vec4<T>
Source§fn div_assign(&mut self, other: Self)
fn div_assign(&mut self, other: Self)
/= operation. Read moreSource§impl<T> DivAssign<T> for Vec4<T>
impl<T> DivAssign<T> for Vec4<T>
Source§fn div_assign(&mut self, scalar: T)
fn div_assign(&mut self, scalar: T)
/= operation. Read moreSource§impl<T> From<(T, T, T, T)> for Vec4<T>
impl<T> From<(T, T, T, T)> for Vec4<T>
Source§fn from(tuple: (T, T, T, T)) -> Self
fn from(tuple: (T, T, T, T)) -> Self
Source§impl<T> Into<(T, T, T, T)> for Vec4<T>
impl<T> Into<(T, T, T, T)> for Vec4<T>
Source§fn into(self) -> (T, T, T, T)
fn into(self) -> (T, T, T, T)
Source§impl<'a, T> IntoIterator for &'a Vec4<T>
impl<'a, T> IntoIterator for &'a Vec4<T>
Source§impl<'a, T> IntoIterator for &'a mut Vec4<T>
impl<'a, T> IntoIterator for &'a mut Vec4<T>
Source§impl<T> MulAssign for Vec4<T>
impl<T> MulAssign for Vec4<T>
Source§fn mul_assign(&mut self, other: Self)
fn mul_assign(&mut self, other: Self)
*= operation. Read moreSource§impl<T> MulAssign<T> for Vec4<T>
impl<T> MulAssign<T> for Vec4<T>
Source§fn mul_assign(&mut self, scalar: T)
fn mul_assign(&mut self, scalar: T)
*= operation. Read more