Skip to main content

Vector2D

Struct Vector2D 

Source
pub struct Vector2D { /* private fields */ }
Expand description

A 2D vector with x and y components represented as f64.

Implementations§

Source§

impl Vector2D

Implements methods and operator overloading for Vector2D.

Source

pub fn zero() -> Vector2D

Returns the zero vector (0.0, 0.0).

§Returns
  • Vector2D - The zero vector.
Source

pub fn right() -> Vector2D

Returns the unit vector pointing right (1.0, 0.0).

§Returns
  • Vector2D - The right unit vector.
Source

pub fn up() -> Vector2D

Returns the unit vector pointing up (0.0, -1.0).

In screen coordinates where y increases downward.

§Returns
  • Vector2D - The up unit vector.
Source

pub fn from_angle(radians: f64) -> Vector2D

Creates a unit vector from an angle in radians.

§Arguments
  • f64 - The angle in radians.
§Returns
  • Vector2D - The unit vector pointing in the given direction.
Source

pub fn magnitude(&self) -> f64

Returns the magnitude (length) of the vector.

§Returns
  • f64 - The magnitude of the vector.
Source

pub fn magnitude_squared(&self) -> f64

Returns the squared magnitude of the vector.

Avoids a square root, making it faster for comparison-only use cases.

§Returns
  • f64 - The squared magnitude of the vector.
Source

pub fn normalized(&self) -> Vector2D

Returns a normalized (unit length) copy of this vector.

Returns the zero vector if the magnitude is zero.

§Returns
  • Vector2D - The normalized vector.
Source

pub fn normalize(&mut self)

Normalizes this vector in place.

Source

pub fn dot(&self, other: Vector2D) -> f64

Computes the dot product with another vector.

§Arguments
  • Vector2D - The other vector.
§Returns
  • f64 - The dot product.
Source

pub fn cross(&self, other: Vector2D) -> f64

Computes the 2D cross product (scalar) with another vector.

§Arguments
  • Vector2D - The other vector.
§Returns
  • f64 - The cross product scalar.
Source

pub fn perp(&self) -> Vector2D

Returns the perpendicular vector (rotated 90 degrees counter-clockwise).

§Returns
  • Vector2D - The perpendicular vector.
Source

pub fn angle(&self) -> f64

Returns the angle of this vector in radians.

§Returns
  • f64 - The angle in radians.
Source

pub fn angle_to(&self, other: Vector2D) -> f64

Returns the angle from this vector to another.

§Arguments
  • Vector2D - The target vector.
§Returns
  • f64 - The signed angle in radians.
Source

pub fn rotated(&self, radians: f64) -> Vector2D

Returns a rotated copy of this vector.

§Arguments
  • f64 - The rotation angle in radians.
§Returns
  • Vector2D - The rotated vector.
Source

pub fn rotate(&mut self, radians: f64)

Rotates this vector in place.

§Arguments
  • f64 - The rotation angle in radians.
Source

pub fn distance_to(&self, other: Vector2D) -> f64

Returns the distance from this point to another.

§Arguments
  • Vector2D - The target point.
§Returns
  • f64 - The Euclidean distance.
Source

pub fn distance_squared_to(&self, other: Vector2D) -> f64

Returns the squared distance from this point to another.

§Arguments
  • Vector2D - The target point.
§Returns
  • f64 - The squared Euclidean distance.
Source

pub fn direction_to(&self, other: Vector2D) -> Vector2D

Returns a unit vector pointing from this point to another.

§Arguments
  • Vector2D - The target point.
§Returns
  • Vector2D - The direction unit vector.
Source

pub fn lerp(&self, other: Vector2D, t: f64) -> Vector2D

Returns a linearly interpolated vector between this and another.

§Arguments
  • Vector2D - The target vector.
  • f64 - The interpolation factor.
§Returns
  • Vector2D - The interpolated vector.
Source

pub fn scale(&mut self, scalar: f64)

Scales this vector by a scalar factor.

§Arguments
  • f64 - The scalar factor.
Source

pub fn scaled(&self, scalar: f64) -> Vector2D

Returns a scaled copy of this vector.

§Arguments
  • f64 - The scalar factor.
§Returns
  • Vector2D - The scaled vector.
Source§

impl Vector2D

Source

pub fn get_x(&self) -> f64

Source

pub fn get_mut_x(&mut self) -> &mut f64

Source

pub fn set_x(&mut self, val: f64) -> &mut Self

Source

pub fn get_y(&self) -> f64

Source

pub fn get_mut_y(&mut self) -> &mut f64

Source

pub fn set_y(&mut self, val: f64) -> &mut Self

Source§

impl Vector2D

Source

pub fn new(x: f64, y: f64) -> Self

Trait Implementations§

Source§

impl Add for Vector2D

Implements vector addition.

Source§

type Output = Vector2D

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl AddAssign for Vector2D

Implements in-place vector addition.

Source§

fn add_assign(&mut self, other: Vector2D)

Performs the += operation. Read more
Source§

impl Clone for Vector2D

Source§

fn clone(&self) -> Vector2D

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 Vector2D

Source§

impl Debug for Vector2D

Source§

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

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

impl Default for Vector2D

Source§

fn default() -> Vector2D

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

impl Interpolable for Vector2D

Implements Interpolable for Vector2D.

Source§

fn lerp(&self, other: Vector2D, t: f64) -> Vector2D

Performs linear interpolation between self and other by the factor t. Read more
Source§

impl Mul<f64> for Vector2D

Implements scalar multiplication.

Source§

type Output = Vector2D

The resulting type after applying the * operator.
Source§

fn mul(self, scalar: f64) -> Vector2D

Performs the * operation. Read more
Source§

impl MulAssign<f64> for Vector2D

Implements in-place scalar multiplication.

Source§

fn mul_assign(&mut self, scalar: f64)

Performs the *= operation. Read more
Source§

impl Neg for Vector2D

Implements vector negation.

Source§

type Output = Vector2D

The resulting type after applying the - operator.
Source§

fn neg(self) -> Vector2D

Performs the unary - operation. Read more
Source§

impl PartialEq for Vector2D

Source§

fn eq(&self, other: &Vector2D) -> 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 PartialOrd for Vector2D

Source§

fn partial_cmp(&self, other: &Vector2D) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StructuralPartialEq for Vector2D

Source§

impl Sub for Vector2D

Implements vector subtraction.

Source§

type Output = Vector2D

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl SubAssign for Vector2D

Implements in-place vector subtraction.

Source§

fn sub_assign(&mut self, other: Vector2D)

Performs the -= operation. Read more

Auto Trait Implementations§

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, 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.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more