开发者

Angle between two 2d vectors, diff between two methods?

开发者 https://www.devze.com 2022-12-27 13:38 出处:网络
I\'ve got this code snippet, and I\'m wondering why the results of the first method differ from the results of the second method, given the same input?

I've got this code snippet, and I'm wondering why the results of the first method differ from the results of the second method, given the same input?

public double AngleBetween_1(vector a, vector b) {
  var dotProd = a.Dot(b);
  var lenProd = a.Len*b.Len;
  var divOperation = dotProd/lenProd;
  retu开发者_运维问答rn Math.Acos(divOperation) * (180.0 / Math.PI);
}

public double AngleBetween_2(vector a, vector b) {
  var dotProd = a.Dot(b);
  var lenProd = a.Len*b.Len;
  var divOperation = dotProd/lenProd;
  return (1/Math.Cos(divOperation)) * (180.0 / Math.PI);
}


It's because the first method is correct, while the second method is incorrect.

You may notice that the arccosine function is sometimes written "acos" and sometimes written "cos-1". This is a quirk of mathematical notation: "cos-1" is really the arccosine and NOT the reciprocal of the cosine (which is the secant).

However, if you ever see "cos2", then that's the square of the cosine, and "cos3" is the cube of the cosine. The notation for trigonometric functions is weird this way. Most operators use superscripts to indicate repeated application.


Math.Acos(divOperation) isn't equivalent to 1/Math.Cos(divOperation). arccos is the inverse function of cos, not the multiplicative inverse.


Probably because acos(x) ≠ 1/cos(x).

0

精彩评论

暂无评论...
验证码 换一张
取 消