Merge pull request #2433 from hannobraun/angle

Fix panic when computing angle with zero vector
This commit is contained in:
Hanno Braun 2024-07-29 19:51:53 +02:00 committed by GitHub
commit 1dbd431f94
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -88,8 +88,15 @@ impl<const D: usize> Vector<D> {
}
/// Compute the angle between this vector and another
///
/// Returns a zero angle, if the magnitude of `self` or `other` is zero.
pub fn angle_to(&self, other: &Self) -> Scalar {
(self.dot(other) / (self.magnitude() * other.magnitude())).acos()
let product = self.magnitude() * other.magnitude();
if product.is_zero() {
Scalar::ZERO
} else {
(self.dot(other) / product).acos()
}
}
/// Compute the dot product with another vector