Trait Point

Source
pub trait Point: Sized + PartialEq {
    // Required methods
    fn distance(&self, other: &Self) -> f64;
    fn move_towards(&self, other: &Self, d: f64) -> Self;

    // Provided method
    fn midpoint(a: &Self, b: &Self) -> Self { ... }
}
Expand description

A Point is something that exists in some sort of metric space, and can thus calculate its distance to another Point, and can be moved a certain distance towards another Point.

Required Methods§

Source

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

Distances should be positive, finite f64s. It is undefined behavior to return a negative, infinite, or NaN result.

Distance should satisfy the triangle inequality. That is, a.distance(c) must be less or equal to than a.distance(b) + b.distance(c).

Source

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

If d is 0, a point equal to the self should be returned. If d is equal to self.distance(other), a point equal to other should be returned. Intermediate distances should be linearly interpolated between the two points, so if d is equal to self.distance(other) / 2.0, the midpoint should be returned. It is undefined behavior to use a distance that is negative, NaN, or greater than self.distance(other).

Provided Methods§

Source

fn midpoint(a: &Self, b: &Self) -> Self

The midpoint between two points is the point that is equidistant from both. It should be equivalent to a.move_towards(b, a.distance(b) / 2.0), but may be implemented more efficiently for some types.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<const D: usize> Point for [f64; D]

Implement Point in the normal D dimensional Euclidean way for all arrays of floats. For example, a 2D point would be a [f64; 2].

Source§

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

Source§

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

Source§

fn midpoint(a: &Self, b: &Self) -> Self

Implementors§