Advertisement

What's the fastest way to calculate a vector angle?

Started by December 24, 2000 02:14 PM
2 comments, last by WhatEver 24 years, 1 month ago
I''ve been using atan. I know there is a faster way.
If you want exact results, atan is the fastest.
But if you only need approximations, you can make a 2d lookup-table and use it.
Advertisement
Thank you. I''ll see how the lookup table works out for me.
The cosine of the angle between two vectors (in any number of dimensions) is equal to the dot product of the unit vectors. This would probably be less efficient than atan in 2d, but it ought to come in handy in 3d.

(untested and unoptimised)

  double Angle(Vector v1, Vector v2){    double v1a = sqrt(v1.x*v1.x + v1.y*v1.y);    double v2a = sqrt(v2.x*v2.x + v2.y*v2.y);    Vector v1u = {v1.x/v1a, v1y/v1a};    Vector v2u = {v2.x/v2a, v2y/v2a};    double dotPr = v1u.x*v2u.x + v1u.y*v2u.y;    return acos(dotPr);}  

This topic is closed to new replies.

Advertisement