Skip to main content

Vector2

Struct Vector2 

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

A 2D vector represented with f64 components.

Implementations§

Source§

impl Vector2

Source

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

Creates a vector from x and y components.

Source

pub const fn x(&self) -> f64

Returns the horizontal component.

Source

pub const fn y(&self) -> f64

Returns the vertical component.

Source

pub const fn try_new(x: f64, y: f64) -> Result<Self, GeometryError>

Creates a vector from finite x and y components.

§Errors

Returns GeometryError::NonFiniteComponent when x or y is NaN or infinite.

§Examples
use use_geometry::{GeometryError, Vector2};

let vector = Vector2::try_new(1.0, -2.0)?;
assert_eq!(vector, Vector2::new(1.0, -2.0));

assert!(matches!(
    Vector2::try_new(1.0, f64::INFINITY),
    Err(GeometryError::NonFiniteComponent { component: "y", .. })
));
Source

pub const fn validate(self) -> Result<Self, GeometryError>

Validates that an existing vector contains only finite components.

§Errors

Returns GeometryError::NonFiniteComponent when self.x or self.y is NaN or infinite.

§Examples
use use_geometry::{GeometryError, Vector2};

let validated = Vector2::new(3.0, 4.0).validate()?;
assert_eq!(validated, Vector2::new(3.0, 4.0));
Source

pub const fn is_finite(self) -> bool

Returns true when both components are finite.

Source

pub const fn zero() -> Self

Returns the zero vector.

Source

pub const fn from_points(a: Point2, b: Point2) -> Self

Returns the vector from point a to point b.

§Examples
use use_geometry::{Point2, Vector2};

let start = Point2::new(1.0, 2.0);
let end = Point2::new(4.0, 6.0);

assert_eq!(Vector2::from_points(start, end), Vector2::new(3.0, 4.0));
Source

pub fn try_from_points(a: Point2, b: Point2) -> Result<Self, GeometryError>

Returns the vector from point a to point b when both points are finite.

§Errors

Returns GeometryError::NonFiniteComponent when either point contains a non-finite coordinate or the resulting vector contains a non-finite component.

§Examples
use use_geometry::{GeometryError, Point2, Vector2};

let vector = Vector2::try_from_points(Point2::new(1.0, 2.0), Point2::new(4.0, 6.0))?;
assert_eq!(vector, Vector2::new(3.0, 4.0));
Source

pub fn magnitude(self) -> f64

Returns the vector magnitude.

§Examples
use use_geometry::Vector2;

let vector = Vector2::new(3.0, 4.0);
assert_eq!(vector.magnitude(), 5.0);
Source

pub fn length(self) -> f64

Returns the vector length.

§Examples
use use_geometry::Vector2;

let vector = Vector2::new(5.0, 12.0);
assert_eq!(vector.length(), 13.0);
Source

pub fn magnitude_squared(self) -> f64

Returns the squared vector magnitude.

§Examples
use use_geometry::Vector2;

let vector = Vector2::new(5.0, 12.0);
assert_eq!(vector.magnitude_squared(), 169.0);
Source

pub fn length_squared(self) -> f64

Returns the squared vector length.

§Examples
use use_geometry::Vector2;

let vector = Vector2::new(5.0, 12.0);
assert_eq!(vector.length_squared(), 169.0);
Source

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

Returns the dot product with another vector.

§Examples
use use_geometry::Vector2;

let left = Vector2::new(1.0, 3.0);
let right = Vector2::new(2.0, 4.0);

assert_eq!(left.dot(right), 14.0);
Source

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

Returns the scalar z-component of the 2D cross product.

§Examples
use use_geometry::Vector2;

let x_axis = Vector2::new(1.0, 0.0);
let y_axis = Vector2::new(0.0, 1.0);

assert_eq!(x_axis.cross(y_axis), 1.0);
Source

pub const fn scale(self, factor: f64) -> Self

Returns a scaled vector.

§Examples
use use_geometry::Vector2;

let vector = Vector2::new(2.0, -3.0);
assert_eq!(vector.scale(0.5), Vector2::new(1.0, -1.5));
Source

pub fn try_normalize(self) -> Option<Self>

Returns a unit-length vector when normalization succeeds.

Returns None for the zero vector and for vectors whose length is not finite.

§Examples
use use_geometry::Vector2;

let unit = Vector2::new(3.0, 4.0).try_normalize().expect("unit vector");

assert!((unit.length() - 1.0).abs() < 1.0e-10);
assert!(Vector2::zero().try_normalize().is_none());
Source

pub fn normalize_or_zero(self) -> Self

Returns a unit-length vector, or zero when normalization fails.

§Examples
use use_geometry::Vector2;

let unit = Vector2::new(3.0, 4.0).normalize_or_zero();

assert!((unit.length() - 1.0).abs() < 1.0e-10);
assert_eq!(Vector2::zero().normalize_or_zero(), Vector2::zero());

Trait Implementations§

Source§

impl Add<Vector2> for Point2

Source§

type Output = Point2

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Vector2) -> Self::Output

Performs the + operation. Read more
Source§

impl Add for Vector2

Source§

type Output = Vector2

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl Clone for Vector2

Source§

fn clone(&self) -> Vector2

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Vector2

Source§

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

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

impl Div<f64> for Vector2

Source§

type Output = Vector2

The resulting type after applying the / operator.
Source§

fn div(self, rhs: f64) -> Self::Output

Performs the / operation. Read more
Source§

impl Mul<f64> for Vector2

Source§

type Output = Vector2

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: f64) -> Self::Output

Performs the * operation. Read more
Source§

impl PartialEq for Vector2

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · 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 Sub<Vector2> for Point2

Source§

type Output = Point2

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Vector2) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub for Vector2

Source§

type Output = Vector2

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl Copy for Vector2

Source§

impl StructuralPartialEq for Vector2

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.