Skip to main content

Vector

Trait Vector 

Source
pub trait Vector:
    Copy
    + Clone
    + Default
    + Debug
    + PartialEq
    + Add<Output = Self>
    + Sub<Output = Self>
    + Mul<f64, Output = Self>
    + Neg<Output = Self>
    + AddAssign
    + SubAssign
    + MulAssign<f64> {
    // Required methods
    fn zero() -> Self;
    fn dot(&self, other: Self) -> f64;
    fn magnitude(&self) -> f64;
    fn magnitude_squared(&self) -> f64;
    fn normalized(&self) -> Self;
    fn scaled(&self, scalar: f64) -> Self;
    fn lerp(&self, other: Self, t: f64) -> Self;
}
Expand description

A trait abstracting the operations common to Vector2D and Vector3D.

Provides a single abstraction surface so that dimension-agnostic algorithms (e.g. integrate_linear in physics) can be written once. Method signatures match the inherent implementations on both vectors: taking &self and returning new owned values where the inherent methods do, taking self by value where they already do.

The arithmetic operator supertraits (Add, Sub, Mul<f64>, Neg, and the *Assign variants) are required so generic code can use natural a + b, v * 2.0, -v, v += other, v *= scalar syntax without importing additional bounds.

Vector2D adds 2D-specific operations (perp, cross -> f64, from_angle) and Vector3D adds 3D-specific ones (cross -> Vector3D). These are intentionally not part of the trait — they have different return types or only exist in one dimension.

Required Methods§

Source

fn zero() -> Self

Returns the additive identity (Self::default()).

§Returns
  • Self - The zero vector for this dimension.
Source

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

Computes the dot product with another vector.

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

fn magnitude(&self) -> f64

Returns the magnitude (length) of the vector.

§Returns
  • f64 - The magnitude.
Source

fn magnitude_squared(&self) -> f64

Returns the squared magnitude of the vector.

Faster than magnitude for comparison-only use cases because it avoids a square root.

§Returns
  • f64 - The squared magnitude.
Source

fn normalized(&self) -> Self

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

Implementations may return the zero vector when the magnitude is below an epsilon threshold.

§Returns
  • Self - The normalized vector.
Source

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

Returns a copy of this vector scaled by the given factor.

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

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

Performs linear interpolation between self and other by t.

§Arguments
  • &Self - The target vector.
  • f64 - The interpolation factor, typically in the range 0.0 to 1.0.
§Returns
  • Self - The interpolated vector.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl Vector for Vector2D

Implements the Vector trait for Vector2D, forwarding every method to the inherent implementation on the struct.

Vector2D also offers 2D-specific operations that are not part of the trait surface: perp, cross (returning f64), from_angle, angle, angle_to, rotated, rotate, distance_to, distance_squared_to, direction_to, scale, and normalize. These remain inherent.

Source§

impl Vector for Vector3D

Implements the Vector trait for Vector3D, forwarding every method to the inherent implementation on the struct.

Vector3D also offers 3D-specific operations that are not part of the trait surface: cross (returning Vector3D), direction_to, distance_to, scale, and normalize. These remain inherent.