From 503040b8bce4956b1d54e824819fcf7b89976763 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Crozet=20S=C3=A9bastien?= Date: Sun, 25 Oct 2020 11:24:05 +0100 Subject: [PATCH] Add Point.map(f) and Point.apply(f). --- src/geometry/point.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/geometry/point.rs b/src/geometry/point.rs index d12b9212..e0c26054 100644 --- a/src/geometry/point.rs +++ b/src/geometry/point.rs @@ -102,6 +102,45 @@ impl Point where DefaultAllocator: Allocator, { + /// 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>(&self, f: F) -> Point + where + DefaultAllocator: Allocator, + { + 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 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 /// end of it. ///