Cheap algorithm to find measure of angle between vectors

前端 未结 9 1590
旧时难觅i
旧时难觅i 2020-12-13 01:02

Finding the angle between two vectors is not hard using the cosine rule. However, because I am programming for a platform with very limited resources, I would like to avoid

9条回答
  •  無奈伤痛
    2020-12-13 01:14

    dot product of two vectors (x1, y1) and (x2, y2) is

    x1 * x2 + y1 * y2 
    

    and is equivilent to the product of the lengths of the two vectors times the cosine of the angle between them.

    So if you normalize the two vectors first (divide the coordinates by the length)

    Where length of V1 L1 = sqrt(x1^2 + y1^2),  
      and length of V2 L2 = sqrt(x2^2 + y2^2),
    

    Then normalized vectors are

    (x1/L1, y1/L1),  and (x2/L2, y2/L2),  
    

    And dot product of normalized vectors (which is the same as the cosine of angle between the vectors) would be

     (x1*x2 + y1*y2)
     -----------------
         (L1*L2)
    

    of course this may be just as computationally difficult as calculating the cosine

提交回复
热议问题