Skip to content

Commit 5f1eeee

Browse files
committed
Added vector angle formula
1 parent 1966da7 commit 5f1eeee

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

math/geometry.h

+9
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ T Cross(const Eigen::Matrix<T, 2, 1>& v1, const Eigen::Matrix<T, 2, 1>& v2) {
5858
return (v1.x() * v2.y() - v2.x() * v1.y());
5959
}
6060

61+
// Return the angle between two vectors in radians
62+
template <typename T>
63+
float VectorAngle(const Eigen::Matrix<T, 2, 1>& v1, const Eigen::Matrix<T, 2, 1>& v2) {
64+
if (v1.norm() < kEpsilon || v2.norm() < kEpsilon) {
65+
return 0; // Degenerate case
66+
}
67+
return acos(v1.dot(v2) / (v1.norm() * v2.norm()));
68+
}
69+
6170
template <typename T, int N>
6271
Eigen::Matrix<T, N, 1> GetNormalizedOrZero(const Eigen::Matrix<T, N, 1>& vec) {
6372
const auto norm = vec.template lpNorm<1>();

0 commit comments

Comments
 (0)