Struct primitives::foundation::Vector2D
source · [−]#[repr(C)]pub struct Vector2D<T> {
pub x: T,
pub y: T,
}
Expand description
A 2d Vector tagged with a unit.
Fields
x: T
The x
(traditionally, horizontal) coordinate.
y: T
The y
(traditionally, vertical) coordinate.
Implementations
sourceimpl<T> Vector2D<T>
impl<T> Vector2D<T>
sourcepub fn splat(v: T) -> Self where
T: Clone,
pub fn splat(v: T) -> Self where
T: Clone,
Constructor setting all components to the same value.
sourcepub fn abs(self) -> Self where
T: Signed,
pub fn abs(self) -> Self where
T: Signed,
Computes the vector with absolute values of each component.
Example
enum U {}
assert_eq!(Vector2D::new(-1, 2).abs(), Vector2D::new(1, 2));
let vec = Vector2D::new(f32::NAN, -f32::MAX).abs();
assert!(vec.x.is_nan());
assert_eq!(vec.y, f32::MAX);
Panics
The behavior for each component follows the scalar type’s implementation of
num_traits::Signed::abs
.
sourcepub fn cross(self, other: Self) -> T where
T: Sub<Output = T> + Mul<Output = T>,
pub fn cross(self, other: Self) -> T where
T: Sub<Output = T> + Mul<Output = T>,
Returns the norm of the cross product [self.x, self.y, 0] x [other.x, other.y, 0].
sourcepub fn component_mul(self, other: Self) -> Self where
T: Mul<Output = T>,
pub fn component_mul(self, other: Self) -> Self where
T: Mul<Output = T>,
Returns the component-wise multiplication of the two vectors.
sourcepub fn component_div(self, other: Self) -> Self where
T: Div<Output = T>,
pub fn component_div(self, other: Self) -> Self where
T: Div<Output = T>,
Returns the component-wise division of the two vectors.
sourceimpl<T> Vector2D<T> where
T: Copy + Mul<T, Output = T> + Add<T, Output = T>,
impl<T> Vector2D<T> where
T: Copy + Mul<T, Output = T> + Add<T, Output = T>,
sourcepub fn square_length(self) -> T
pub fn square_length(self) -> T
Returns the vector’s length squared.
sourcepub fn project_onto_vector(self, onto: Self) -> Self where
T: Sub<T, Output = T> + Div<T, Output = T>,
pub fn project_onto_vector(self, onto: Self) -> Self where
T: Sub<T, Output = T> + Div<T, Output = T>,
Returns this vector projected onto another one.
Projecting onto a nil vector will cause a division by zero.
sourceimpl<T: Float> Vector2D<T>
impl<T: Float> Vector2D<T>
sourcepub fn try_normalize(self) -> Option<Self>
pub fn try_normalize(self) -> Option<Self>
Returns the vector with length of one unit.
Unlike Vector2D::normalize
, this returns None in the case that the
length of the vector is zero.
sourcepub fn robust_normalize(self) -> Self
pub fn robust_normalize(self) -> Self
Return the normalized vector even if the length is larger than the max value of Float.
sourcepub fn with_max_length(self, max_length: T) -> Self
pub fn with_max_length(self, max_length: T) -> Self
Return this vector capped to a maximum length.
sourcepub fn with_min_length(self, min_length: T) -> Self
pub fn with_min_length(self, min_length: T) -> Self
Return this vector with a minimum length applied.
sourcepub fn clamp_length(self, min: T, max: T) -> Self
pub fn clamp_length(self, min: T, max: T) -> Self
Return this vector with minimum and maximum lengths applied.
sourceimpl<T: NumCast + Copy> Vector2D<T>
impl<T: NumCast + Copy> Vector2D<T>
sourcepub fn cast<NewT: NumCast>(self) -> Vector2D<NewT>
pub fn cast<NewT: NumCast>(self) -> Vector2D<NewT>
Cast from one numeric representation to another, preserving the units.
When casting from floating vector to integer coordinates, the decimals are truncated
as one would expect from a simple cast, but this behavior does not always make sense
geometrically. Consider using round()
, ceil()
or floor()
before casting.
sourcepub fn try_cast<NewT: NumCast>(self) -> Option<Vector2D<NewT>>
pub fn try_cast<NewT: NumCast>(self) -> Option<Vector2D<NewT>>
Fallible cast from one numeric representation to another, preserving the units.
When casting from floating vector to integer coordinates, the decimals are truncated
as one would expect from a simple cast, but this behavior does not always make sense
geometrically. Consider using round()
, ceil()
or floor()
before casting.
sourcepub fn to_usize(self) -> Vector2D<usize>
pub fn to_usize(self) -> Vector2D<usize>
Cast into an usize
vector, truncating decimals if any.
When casting from floating vector vectors, it is worth considering whether
to round()
, ceil()
or floor()
before the cast in order to obtain
the desired conversion behavior.
sourcepub fn to_u32(self) -> Vector2D<u32>
pub fn to_u32(self) -> Vector2D<u32>
Cast into an u32
vector, truncating decimals if any.
When casting from floating vector vectors, it is worth considering whether
to round()
, ceil()
or floor()
before the cast in order to obtain
the desired conversion behavior.
Trait Implementations
sourceimpl<T: Copy + Add<T, Output = T>> AddAssign<Vector2D<T>> for Vector2D<T>
impl<T: Copy + Add<T, Output = T>> AddAssign<Vector2D<T>> for Vector2D<T>
sourcefn add_assign(&mut self, other: Self)
fn add_assign(&mut self, other: Self)
Performs the +=
operation. Read more
sourceimpl<T: Copy + Div<T, Output = T>> DivAssign<T> for Vector2D<T>
impl<T: Copy + Div<T, Output = T>> DivAssign<T> for Vector2D<T>
sourcefn div_assign(&mut self, scale: T)
fn div_assign(&mut self, scale: T)
Performs the /=
operation. Read more
sourceimpl<T: Copy + Mul<T, Output = T>> MulAssign<T> for Vector2D<T>
impl<T: Copy + Mul<T, Output = T>> MulAssign<T> for Vector2D<T>
sourcefn mul_assign(&mut self, scale: T)
fn mul_assign(&mut self, scale: T)
Performs the *=
operation. Read more
sourceimpl<T: Copy + Sub<T, Output = T>> SubAssign<Vector2D<T>> for Vector2D<T>
impl<T: Copy + Sub<T, Output = T>> SubAssign<Vector2D<T>> for Vector2D<T>
sourcefn sub_assign(&mut self, other: Self)
fn sub_assign(&mut self, other: Self)
Performs the -=
operation. Read more
impl<T: Copy> Copy for Vector2D<T>
impl<T: Eq> Eq for Vector2D<T>
Auto Trait Implementations
impl<T> RefUnwindSafe for Vector2D<T> where
T: RefUnwindSafe,
impl<T> Send for Vector2D<T> where
T: Send,
impl<T> Sync for Vector2D<T> where
T: Sync,
impl<T> Unpin for Vector2D<T> where
T: Unpin,
impl<T> UnwindSafe for Vector2D<T> where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more