Skip to main content

path_traits/
point.rs

1//! Points and vectors for geometric computations.
2//!
3//! This module defines the foundational geometric primitives:
4//!
5//! - [`Vector`] - a type in a linear (vector) space with algebraic operations
6//!   (`Add`, `Sub`, `Mul` by scalar) plus `dot` and `norm`.
7//! - [`Point`] - a type in an affine space with `displacement` (→ [`Vector`]) and
8//!   `translate` (← [`Vector`]), plus a default `distance` implementation.
9//!
10//! Points and vectors are kept distinct because a position on a path is a point,
11//! while derivatives (tangent, curvature vector) are vectors.
12
13use crate::Scalar;
14
15/// A displacement or derivative in Euclidean space.
16///
17/// Vectors represent displacements and derivatives (e.g. tangent vectors).
18/// They form a linear space: addition, subtraction, and scalar multiplication
19/// are required, along with `dot` product and Euclidean `norm`.
20pub trait Vector:
21    Copy
22    + core::ops::Add<Output = Self>
23    + core::ops::Sub<Output = Self>
24    + core::ops::Mul<<Self as Vector>::Scalar, Output = Self>
25{
26    /// The scalar type used for components and magnitudes.
27    type Scalar: Scalar;
28
29    /// The zero vector (additive identity).
30    fn zero() -> Self;
31
32    /// Dot product of two vectors.
33    fn dot(self, rhs: Self) -> Self::Scalar;
34
35    /// Euclidean norm (magnitude) of the vector.
36    fn norm(self) -> Self::Scalar;
37}
38
39/// A position in an affine space, parameterized by its scalar and vector types.
40///
41/// Points represent positions. They cannot be added, but a displacement from one
42/// point to another yields a [`Vector`], and translating a point by a [`Vector`]
43/// yields another point.
44pub trait Point: Copy {
45    /// Scalar type for distances and coordinates.
46    type Scalar: Scalar;
47
48    /// Vector type for displacements and derivatives.
49    type Vector: Vector<Scalar = Self::Scalar>;
50
51    /// Displacement from `self` to `other` (i.e. `other - self`).
52    fn displacement(self, other: Self) -> Self::Vector;
53
54    /// Translate this point by a vector, returning a new point.
55    fn translate(self, v: Self::Vector) -> Self;
56
57    /// Euclidean distance between two points.
58    fn distance(self, other: Self) -> Self::Scalar {
59        self.displacement(other).norm()
60    }
61}