Add Vector::dot

This commit is contained in:
Hanno Braun 2025-01-09 18:34:13 +01:00
parent 878f6051bc
commit 10a2036740

View File

@ -11,17 +11,21 @@ pub struct Vector<const D: usize> {
impl<const D: usize> Vector<D> {
pub fn magnitude(&self) -> Scalar {
self.components
.into_iter()
.map(|coord| coord * coord)
.reduce(|a, b| a + b)
.unwrap_or(Scalar::zero())
.sqrt()
self.dot(self).sqrt()
}
pub fn normalize(self) -> Self {
self / self.magnitude()
}
pub fn dot(&self, other: &Self) -> Scalar {
self.components
.into_iter()
.zip(other.components)
.map(|(a, b)| a * b)
.reduce(|a, b| a + b)
.unwrap_or(Scalar::zero())
}
}
impl Vector<3> {