]> git.nega.tv - josh/narcissus/commitdiff
Add `normalized` function to vectors
authorJoshua Simmons <josh@nega.tv>
Sun, 11 Sep 2022 14:01:51 +0000 (16:01 +0200)
committerJoshua Simmons <josh@nega.tv>
Sun, 11 Sep 2022 14:01:51 +0000 (16:01 +0200)
narcissus-maths/src/lib.rs

index c2fc7b3c4a04ed7beca7c7eb5c1bd7766af26b4a..3193cd329c1566cdcd20d3f3eada22a1397de66c 100644 (file)
@@ -230,6 +230,13 @@ macro_rules! impl_vector {
             pub fn length_sq(self) -> $t {
                 Self::dot(self, self)
             }
+
+            /// Returns a vector with the same direction as `self` but with unit (1.0) length.
+            #[must_use]
+            #[inline]
+            pub fn normalized(self) -> $name {
+                self / self.length()
+            }
         }
 
         impl std::ops::Neg for $name {