pub struct Point2 { /* private fields */ }Expand description
A 2D point represented with f64 coordinates.
Implementations§
Source§impl Point2
impl Point2
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 point from finite x and y coordinates.
§Errors
Returns GeometryError::NonFiniteComponent when x or y is NaN
or infinite.
§Examples
use use_geometry::{GeometryError, Point2};
let point = Point2::try_new(1.0, -2.0)?;
assert_eq!(point, Point2::new(1.0, -2.0));
assert!(matches!(
Point2::try_new(f64::NAN, 0.0),
Err(GeometryError::NonFiniteComponent { component: "x", .. })
));Sourcepub const fn validate(self) -> Result<Self, GeometryError>
pub const fn validate(self) -> Result<Self, GeometryError>
Validates that an existing point contains only finite coordinates.
§Errors
Returns GeometryError::NonFiniteComponent when self.x or
self.y is NaN or infinite.
§Examples
use use_geometry::{GeometryError, Point2};
let validated = Point2::new(3.0, 4.0).validate()?;
assert_eq!(validated, Point2::new(3.0, 4.0));Sourcepub fn distance_to(self, other: Self) -> f64
pub fn distance_to(self, other: Self) -> f64
Returns the Euclidean distance to another point.
§Examples
use use_geometry::Point2;
let origin = Point2::new(0.0, 0.0);
let point = Point2::new(3.0, 4.0);
assert_eq!(origin.distance_to(point), 5.0);Sourcepub fn distance_squared_to(self, other: Self) -> f64
pub fn distance_squared_to(self, other: Self) -> f64
Returns the squared Euclidean distance to another point.
§Examples
use use_geometry::Point2;
let left = Point2::new(0.0, 0.0);
let right = Point2::new(3.0, 4.0);
assert_eq!(left.distance_squared_to(right), 25.0);Sourcepub const fn midpoint(self, other: Self) -> Self
pub const fn midpoint(self, other: Self) -> Self
Returns the midpoint between this point and another point.
§Examples
use use_geometry::Point2;
let left = Point2::new(-2.0, 1.0);
let right = Point2::new(4.0, 5.0);
assert_eq!(left.midpoint(right), Point2::new(1.0, 3.0));Sourcepub const fn lerp(self, other: Self, t: f64) -> Self
pub const fn lerp(self, other: Self, t: f64) -> Self
Returns a point interpolated between this point and other.
§Examples
use use_geometry::Point2;
let start = Point2::new(0.0, 0.0);
let end = Point2::new(8.0, 4.0);
assert_eq!(start.lerp(end, 0.25), Point2::new(2.0, 1.0));Trait Implementations§
impl Copy for Point2
impl StructuralPartialEq for Point2
Auto Trait Implementations§
impl Freeze for Point2
impl RefUnwindSafe for Point2
impl Send for Point2
impl Sync for Point2
impl Unpin for Point2
impl UnsafeUnpin for Point2
impl UnwindSafe for Point2
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more