Skip to content

Commit

Permalink
Added vector angle formula
Browse files Browse the repository at this point in the history
  • Loading branch information
artzha committed Jan 14, 2025
1 parent 1966da7 commit 5f1eeee
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions math/geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ T Cross(const Eigen::Matrix<T, 2, 1>& v1, const Eigen::Matrix<T, 2, 1>& v2) {
return (v1.x() * v2.y() - v2.x() * v1.y());
}

// Return the angle between two vectors in radians
template <typename T>
float VectorAngle(const Eigen::Matrix<T, 2, 1>& v1, const Eigen::Matrix<T, 2, 1>& v2) {
if (v1.norm() < kEpsilon || v2.norm() < kEpsilon) {
return 0; // Degenerate case
}
return acos(v1.dot(v2) / (v1.norm() * v2.norm()));
}

template <typename T, int N>
Eigen::Matrix<T, N, 1> GetNormalizedOrZero(const Eigen::Matrix<T, N, 1>& vec) {
const auto norm = vec.template lpNorm<1>();
Expand Down

0 comments on commit 5f1eeee

Please sign in to comment.