Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
  return 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);
}
like image 712
bitcycle Avatar asked Apr 18 '10 23:04

bitcycle


People also ask

How do you find the angle between two vectors A and B?

Formula for angle between two Vectors The cosine of the angle between two vectors is equal to the sum of the product of the individual constituents of the two vectors, divided by the product of the magnitude of the two vectors. =| A | | B | cosθ.

How do you find the angle between two vectors on different planes?

The equations of the two planes in vector form are r.n1 = d1 and r.n2 = d2 and the equations of the two planes in the cartesian form are A1x + B1y + C1z + D1 = 0 and A2x + B2y + C2z + D2 = 0. Then, the formulas to find the angle between two planes are: cos θ = |(n1 .

What is the angle between a B and B A?

Thus, the angle between the vectors a → × b → and b → × a → is 180º.


1 Answers

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.

like image 61
Dietrich Epp Avatar answered Sep 27 '22 19:09

Dietrich Epp