Skip to main content

Vec2

Struct Vec2 

Source
pub struct Vec2 {
    pub x: f32,
    pub y: f32,
}
Expand description

A 2D vector of f32 components.

Fields§

§x: f32

X component.

§y: f32

Y component.

Implementations§

Source§

impl Vec2

Source

pub const ZERO: Vec2

Zero vector.

Source

pub const ONE: Vec2

Vector whose components are all 1.0.

Source

pub const fn new(x: f32, y: f32) -> Vec2

Creates a new vector.

Source

pub const fn as_array(&self) -> [f32; 2]

Returns the components as an array.

Source

pub const fn from_array(v: [f32; 2]) -> Vec2

Creates a vector from an array.

Source

pub fn dot(&self, rhs: &Vec2) -> f32

Dot product.

Source

pub fn norm_sq(&self) -> f32

Squared norm. Infallible, but may be inf or 0 for extreme components; use Self::norm in those cases.

Source

pub fn norm(&self) -> Result<f32, VecError>

Euclidean norm (robust for very large or very small values).

Returns VecError::NonFiniteValue if a component is NaN or infinite, or if the norm exceeds f32::MAX. The norm of the zero vector is Ok(0.0).

Source

pub fn normalize(&self) -> Result<Vec2, VecError>

Unit vector in the same direction.

Returns VecError::ZeroNorm for an exact zero vector and VecError::NonFiniteValue if a component is invalid.

Source

pub fn normalize_or_zero(&self) -> Vec2

Like Self::normalize, but returns Self::ZERO on failure.

Source

pub fn distance_sq(&self, rhs: &Vec2) -> f32

Squared distance (avoids the square root).

Source

pub fn distance(&self, rhs: &Vec2) -> Result<f32, VecError>

Distance euclidienne entre deux vecteurs.

Source

pub fn lerp(&self, rhs: &Vec2, t: f32) -> Vec2

Linear interpolation: self + t * (rhs - self).

Source

pub fn hadamard(&self, rhs: &Vec2) -> Vec2

Component-wise multiplication (Hadamard product).

Source

pub fn min(&self, rhs: &Vec2) -> Vec2

Component-wise minimum.

Source

pub fn max(&self, rhs: &Vec2) -> Vec2

Component-wise maximum.

Source

pub fn abs(&self) -> Vec2

Component-wise absolute value.

Source

pub fn checked_div(&self, s: f32) -> Result<Vec2, VecError>

Divides by a scalar without silently producing inf or NaN.

Returns VecError::DivisionByZero if s == 0.0 and VecError::NonFiniteValue if the result is not finite.

Source

pub fn project_onto(&self, rhs: &Vec2) -> Result<Vec2, VecError>

Projection de self sur rhs : (self·rhs / |rhs|²) * rhs.

Scale-independent for both inputs, avoiding overflow in intermediate dot products and squared norms. Returns VecError::ZeroNorm if rhs is zero, or VecError::NonFiniteValue for non-finite inputs or an unrepresentable result.

Source

pub fn reject_from(&self, rhs: &Vec2) -> Result<Vec2, VecError>

Component of self perpendicular to rhs: self - proj(self, rhs).

Source

pub fn reflect(&self, normal: &Vec2) -> Result<Vec2, VecError>

Reflects self across the plane or line with normal normal.

normal does not need to be a unit vector, but must be nonzero.

Source

pub fn is_finite(&self) -> bool

Returns true if all components are finite.

Source

pub fn is_approx_eq(&self, rhs: &Vec2, eps: f32) -> bool

Approximate equality: each component differs by at most eps.

Source§

impl Vec2

Source

pub const X: Vec2

Unit vector along X: [1, 0].

Source

pub const Y: Vec2

Unit vector along Y: [0, 1].

Source

pub fn cross(&self, rhs: &Vec2) -> f32

2D cross product (the Z component of self × rhs): x₁y₂ - y₁x₂.

Positive when rhs is to the left of self (counterclockwise).

Source

pub const fn perp(&self) -> Vec2

Perpendicular vector (90° counterclockwise rotation): [-y, x].

Source

pub const fn extend(&self, z: f32) -> Vec3

Extends to Vec3 with the z component.

Trait Implementations§

Source§

impl Add for Vec2

Source§

type Output = Vec2

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Vec2) -> Vec2

Performs the + operation. Read more
Source§

impl AddAssign for Vec2

Source§

fn add_assign(&mut self, rhs: Vec2)

Performs the += operation. Read more
Source§

impl Clone for Vec2

Source§

fn clone(&self) -> Vec2

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 Copy for Vec2

Source§

impl Debug for Vec2

Source§

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

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

impl Default for Vec2

Source§

fn default() -> Vec2

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

impl Div<f32> for Vec2

Source§

type Output = Vec2

The resulting type after applying the / operator.
Source§

fn div(self, s: f32) -> Vec2

Performs the / operation. Read more
Source§

impl DivAssign<f32> for Vec2

Source§

fn div_assign(&mut self, s: f32)

Performs the /= operation. Read more
Source§

impl From<[f32; 2]> for Vec2

Source§

fn from(v: [f32; 2]) -> Vec2

Converts to this type from the input type.
Source§

impl Mul<f32> for Vec2

Source§

type Output = Vec2

The resulting type after applying the * operator.
Source§

fn mul(self, s: f32) -> Vec2

Performs the * operation. Read more
Source§

impl MulAssign<f32> for Vec2

Source§

fn mul_assign(&mut self, s: f32)

Performs the *= operation. Read more
Source§

impl Neg for Vec2

Source§

type Output = Vec2

The resulting type after applying the - operator.
Source§

fn neg(self) -> Vec2

Performs the unary - operation. Read more
Source§

impl PartialEq for Vec2

Source§

fn eq(&self, other: &Vec2) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Vec2

Source§

impl Sub for Vec2

Source§

type Output = Vec2

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Vec2) -> Vec2

Performs the - operation. Read more
Source§

impl SubAssign for Vec2

Source§

fn sub_assign(&mut self, rhs: Vec2)

Performs the -= operation. Read more
Source§

impl Sum for Vec2

Source§

fn sum<I>(iter: I) -> Vec2
where I: Iterator<Item = Vec2>,

Takes an iterator and generates Self from the elements by “summing up” the items.

Auto Trait Implementations§

§

impl Forget for Vec2

§

impl Freeze for Vec2

§

impl Move for Vec2

§

impl RefUnwindSafe for Vec2

§

impl Send for Vec2

§

impl Sync for Vec2

§

impl Unpin for Vec2

§

impl UnsafeUnpin for Vec2

§

impl UnwindSafe for Vec2

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.