Signed angle between two vectors without a reference plane

前端 未结 4 984
余生分开走
余生分开走 2021-02-02 00:15

(In three dimensions) I\'m looking for a way to compute the signed angle between two vectors, given no information other than those vectors. As answered in this question, it is

4条回答
  •  醉梦人生
    2021-02-02 00:47

    The relevant mathematical formulas:

      dot_product(a,b) == length(a) * length(b) * cos(angle)
      length(cross_product(a,b)) == length(a) * length(b) * sin(angle)
    

    For a robust angle between 3-D vectors, your actual computation should be:

      s = length(cross_product(a,b))
      c = dot_product(a,b)
      angle = atan2(s, c)
    

    If you use acos(c) alone, you will get severe precision problems for cases when the angle is small. Computing s and using atan2() gives you a robust result for all possible cases.

    Since s is always nonnegative, the resulting angle will range from 0 to pi. There will always be an equivalent negative angle (angle - 2*pi), but there is no geometric reason to prefer it.

提交回复
热议问题