Find the Angle Between Two Vectors

It is usually understood that the angle between two three-dimensional
vectors is measured by the shortest great circle path between them, which
means that it must lie between 0 and pi radians. To get such an answer,
the best method, in my opinion, is this:

 angle = atan2(norm(cross(a,b)),dot(a,b));

double angleBetweenVectors(Eigen::Vector3d a, Eigen::Vector3d b) {

double angle = 0.0;

angle = std::atan2(a.cross(b).norm(),;

return angle;


Since the first argument must be non-negative, the angle will lie
somewhere in the two first quadrants, and thus be between 0 and pi. This
formula remains valid even if a and b are not unit vectors.

Your ‘acos’ formula gives the correct answer with unit vectors as it
stands, but it encounters an accuracy problem for angles that are near 0
or pi. This is the main reason for my preference for the ‘atan2’ method.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s