pub struct Vector2 { /* private fields */ }Expand description
A 2D vector represented with f64 components.
Implementations§
Source§impl Vector2
impl Vector2
Sourcepub const fn try_new(x: f64, y: f64) -> Result<Self, GeometryError>
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", .. })
));Sourcepub const fn validate(self) -> Result<Self, GeometryError>
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));Sourcepub const fn from_points(a: Point2, b: Point2) -> Self
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));Sourcepub fn try_from_points(a: Point2, b: Point2) -> Result<Self, GeometryError>
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));Sourcepub fn magnitude(self) -> f64
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);Sourcepub fn length(self) -> f64
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);Sourcepub fn magnitude_squared(self) -> f64
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);Sourcepub fn length_squared(self) -> f64
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);Sourcepub fn dot(self, other: Self) -> f64
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);Sourcepub fn cross(self, other: Self) -> f64
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);Sourcepub const fn scale(self, factor: f64) -> Self
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));Sourcepub fn try_normalize(self) -> Option<Self>
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());Sourcepub fn normalize_or_zero(self) -> Self
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());