Add Point.map(f) and Point.apply(f).

This commit is contained in:
Crozet Sébastien 2020-10-25 11:24:05 +01:00
parent a623e63d08
commit 503040b8bc
1 changed files with 39 additions and 0 deletions

View File

@ -102,6 +102,45 @@ impl<N: Scalar, D: DimName> Point<N, D>
where where
DefaultAllocator: Allocator<N, D>, DefaultAllocator: Allocator<N, D>,
{ {
/// Returns a point containing the result of `f` applied to each of its entries.
///
/// # Example
/// ```
/// # use nalgebra::{Point2, Point3};
/// let p = Point2::new(1.0, 2.0);
/// assert_eq!(p.map(|e| e * 10.0), Point2::new(10.0, 20.0));
///
/// // This works in any dimension.
/// let p = Point3::new(1.1, 2.1, 3.1);
/// assert_eq!(p.map(|e| e as u32), Point3::new(1, 2, 3));
/// ```
#[inline]
pub fn map<N2: Scalar, F: FnMut(N) -> N2>(&self, f: F) -> Point<N2, D>
where
DefaultAllocator: Allocator<N2, D>,
{
self.coords.map(f).into()
}
/// Replaces each component of `self` by the result of a closure `f` applied on it.
///
/// # Example
/// ```
/// # use nalgebra::{Point2, Point3};
/// let mut p = Point2::new(1.0, 2.0);
/// p.apply(|e| e * 10.0);
/// assert_eq!(p, Point2::new(10.0, 20.0));
///
/// // This works in any dimension.
/// let mut p = Point3::new(1.0, 2.0, 3.0);
/// p.apply(|e| e * 10.0);
/// assert_eq!(p, Point3::new(10.0, 20.0, 30.0));
/// ```
#[inline]
pub fn apply<F: FnMut(N) -> N>(&mut self, f: F) {
self.coords.apply(f)
}
/// Converts this point into a vector in homogeneous coordinates, i.e., appends a `1` at the /// Converts this point into a vector in homogeneous coordinates, i.e., appends a `1` at the
/// end of it. /// end of it.
/// ///