Skip to content

Commit

Permalink
Merge pull request #810 from hannobraun/math
Browse files Browse the repository at this point in the history
Fix edge case in `Vector::scalar_projection_onto`
  • Loading branch information
hannobraun authored Jul 13, 2022
2 parents ca33fd4 + 711999b commit b9012fd
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions crates/fj-math/src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ impl<const D: usize> Vector<D> {

/// Compute the scalar project of this vector onto another
pub fn scalar_projection_onto(&self, other: &Self) -> Scalar {
if other.magnitude() == Scalar::ZERO {
return Scalar::ZERO;
}

self.dot(&other.normalize())
}
}
Expand Down Expand Up @@ -366,5 +370,12 @@ mod tests {
assert_eq!(v.scalar_projection_onto(&x), Scalar::from(1.));
assert_eq!(v.scalar_projection_onto(&y), Scalar::from(2.));
assert_eq!(v.scalar_projection_onto(&z), Scalar::from(3.));

// Zero-length vectors should be handled as well.
assert_eq!(
Vector::unit_x()
.scalar_projection_onto(&Vector::from([0., 0., 0.])),
Scalar::ZERO
);
}
}

0 comments on commit b9012fd

Please sign in to comment.