pub struct Vec2(/* private fields */);Expand description
A 2-component non-owning view of a Euclidean vector.
§Guarantees
This type has the strict requirement that it can only reference a
2-component slice of f32 values. It is guaranteed to never refer to
more or less than 2 entries.
§Relation to Vector2
Vec2 is a non-owning equivalent of Vector2. This enables non-vector
types to either Deref or provide conversion-related utilities into
Vec2 types to be able to access and benefit from vector operations.
A basic example of where this can be useful is in something like a Point2
type, where it may not be implemented directly in terms of a Vector2,
but can convert into a Vec2 to benefit from operations on vectors.
Implementations§
source§impl Vec2
impl Vec2
sourcepub const fn from_array(array: &[f32; 2]) -> &Self
pub const fn from_array(array: &[f32; 2]) -> &Self
Forms a reference to a Vec2 from a 2-component f32 array.
This function is identical to from_slice_unchecked, except it is not
marked unsafe.
§Parameters
array- an array containing 2f32values.
§Examples
Basic use:
let array: [f32; 2] = [0.1, 0.42];
let vec = Vec2::from_array(&array);
assert_eq!(vec.as_ptr(), array.as_ptr());
assert_eq!(vec.x(), array[0]);
assert_eq!(vec.y(), array[1]);sourcepub fn from_mut_array(array: &mut [f32; 2]) -> &mut Self
pub fn from_mut_array(array: &mut [f32; 2]) -> &mut Self
Forms a mutable reference to a Vec2 from a 2-component f32 array.
This function is identical to from_mut_slice_unchecked, except it is
not marked unsafe.
§Parameters
array- an array containing 2f32values.
§Examples
Basic use:
let mut array: [f32; 2] = [0.1, 0.42];
let vec = Vec2::from_mut_array(&mut array);
assert_eq!(vec.as_ptr(), array.as_ptr());sourcepub const fn from_slice(slice: &[f32]) -> Result<&Self, VecError>
pub const fn from_slice(slice: &[f32]) -> Result<&Self, VecError>
Forms a reference to a Vec2 from a slice of f32.
This requires that slice.len() == 2, otherwise this returns VecError.
§Parameters
slice- the slice off32s.
§Examples
Basic use:
let slice = &[0.1, 0.42];
let vec = Vec2::from_slice(slice).unwrap();
assert_eq!(vec.as_ptr(), slice.as_ptr());
assert_eq!(vec.x(), slice[0]);
assert_eq!(vec.y(), slice[1]);Invalid size:
let slice = &[0.1];
let vec = Vec2::from_slice(slice);
assert!(vec.is_err());sourcepub fn from_mut_slice(slice: &mut [f32]) -> Result<&mut Self, VecError>
pub fn from_mut_slice(slice: &mut [f32]) -> Result<&mut Self, VecError>
Forms a mutable reference to a Vec2 from a mutable slice of f32.
This requires that slice.len() == 2, otherwise this returns VecError.
§Parameters
slice- the mutable slice off32s.
§Examples
Basic use:
let slice = &mut [0.1, 0.42];
let vec = Vec2::from_mut_slice(slice).unwrap();
assert_eq!(vec.as_ptr(), slice.as_ptr());Invalid size:
let slice = &mut [0.1];
let vec = Vec2::from_mut_slice(slice);
assert!(vec.is_err());sourcepub const unsafe fn from_slice_unchecked(slice: &[f32]) -> &Self
pub const unsafe fn from_slice_unchecked(slice: &[f32]) -> &Self
sourcepub unsafe fn from_mut_slice_unchecked(slice: &mut [f32]) -> &mut Self
pub unsafe fn from_mut_slice_unchecked(slice: &mut [f32]) -> &mut Self
sourcepub const unsafe fn from_ptr_unchecked<'a>(ptr: *const f32) -> &'a Vec2
pub const unsafe fn from_ptr_unchecked<'a>(ptr: *const f32) -> &'a Vec2
sourcepub unsafe fn from_mut_ptr_unchecked<'a>(ptr: *mut f32) -> &'a mut Vec2
pub unsafe fn from_mut_ptr_unchecked<'a>(ptr: *mut f32) -> &'a mut Vec2
sourcepub fn as_mut_slice(&mut self) -> &mut [f32]
pub fn as_mut_slice(&mut self) -> &mut [f32]
sourcepub fn as_mut_ptr(&mut self) -> *mut f32
pub fn as_mut_ptr(&mut self) -> *mut f32
Returns a raw pointer to f32 of the vector’s buffer.
sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = &mut f32>
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut f32>
Returns a mutable iterator over the vector.
sourcepub fn x_mut(&mut self) -> &mut f32
pub fn x_mut(&mut self) -> &mut f32
Returns a mutable reference to the X-coordinate of this 2-component vector.
sourcepub fn y_mut(&mut self) -> &mut f32
pub fn y_mut(&mut self) -> &mut f32
Returns a mutable reference to the Y-coordinate of this 2-component vector.
sourcepub fn square_magnitude(&self) -> f32
pub fn square_magnitude(&self) -> f32
Computes the square magnitude of this two-component vector.
sourcepub fn magnitude(&self) -> f32
pub fn magnitude(&self) -> f32
Computes the magnitude of this vector.
Where possible, consider using Vec2::square_magnitude as this will
avoid the need to compute the square-root.
sourcepub fn is_normalized(&self) -> bool
pub fn is_normalized(&self) -> bool
Queries whether this vector is normalized (e.g. has a magnitude of 1).
sourcepub fn normalized(&self) -> Vector2
pub fn normalized(&self) -> Vector2
Returns a normalized version of this vector.
sourcepub fn is_finite(&self) -> bool
pub fn is_finite(&self) -> bool
Returns whether all components of this Vec are finite.
§Example
Basic use:
let finite = Vector2::UNIT_X;
let not_finite = Vector2::new(f32::INFINITY, 3.0);
assert!(finite.is_finite());
assert!(!not_finite.is_finite());sourcepub fn is_infinite(&self) -> bool
pub fn is_infinite(&self) -> bool
sourcepub fn is_nan(&self) -> bool
pub fn is_nan(&self) -> bool
Returns whether any component of this vec are nan.
§Example
Basic use:
let not_nan = Vector2::UNIT_X;
let nan = Vector2::new(f32::NAN, 3.0);
assert!(!not_nan.is_nan());
assert!(nan.is_nan());sourcepub fn abs(&self) -> Vector2
pub fn abs(&self) -> Vector2
Computes the absolute value of self
§Example
Basic use:
let vec = Vector2::new(-3.0, 4.0);
let abs = vec.abs();
assert_eq!(abs, Vector2::new(3.0, 4.0));Trait Implementations§
source§impl AddAssign<&Vec2> for Vec2
impl AddAssign<&Vec2> for Vec2
source§fn add_assign(&mut self, rhs: &Vec2)
fn add_assign(&mut self, rhs: &Vec2)
+= operation. Read moresource§impl AddAssign<&Vec2> for Vector2
impl AddAssign<&Vec2> for Vector2
source§fn add_assign(&mut self, rhs: &Vec2)
fn add_assign(&mut self, rhs: &Vec2)
+= operation. Read moresource§impl BorrowMut<[f32]> for Vec2
impl BorrowMut<[f32]> for Vec2
source§fn borrow_mut(&mut self) -> &mut [f32]
fn borrow_mut(&mut self) -> &mut [f32]
source§impl BorrowMut<Vec2> for Vector2
impl BorrowMut<Vec2> for Vector2
source§fn borrow_mut(&mut self) -> &mut Vec2
fn borrow_mut(&mut self) -> &mut Vec2
source§impl DivAssign<f32> for Vec2
impl DivAssign<f32> for Vec2
source§fn div_assign(&mut self, rhs: f32)
fn div_assign(&mut self, rhs: f32)
/= operation. Read moresource§impl From<&Vec2> for Horizontal
impl From<&Vec2> for Horizontal
source§impl MulAssign<f32> for Vec2
impl MulAssign<f32> for Vec2
source§fn mul_assign(&mut self, rhs: f32)
fn mul_assign(&mut self, rhs: f32)
*= operation. Read moresource§impl PartialOrd for Vec2
impl PartialOrd for Vec2
source§impl RemAssign<f32> for Vec2
impl RemAssign<f32> for Vec2
source§fn rem_assign(&mut self, rhs: f32)
fn rem_assign(&mut self, rhs: f32)
%= operation. Read moresource§impl SubAssign<&Vec2> for Vec2
impl SubAssign<&Vec2> for Vec2
source§fn sub_assign(&mut self, rhs: &Vec2)
fn sub_assign(&mut self, rhs: &Vec2)
-= operation. Read moresource§impl SubAssign<&Vec2> for Vector2
impl SubAssign<&Vec2> for Vector2
source§fn sub_assign(&mut self, rhs: &Vec2)
fn sub_assign(&mut self, rhs: &Vec2)
-= operation. Read more