Started by May 10 2005 01:57 AM

,
4 replies to this topic

Posted 10 May 2005 - 01:57 AM

Hi
I have two 2D positions and need angle from one to another. I can easily get to this point:
7 / 3 = tan X
Any1 knows how to get this into C++ code so I can solve X and get a angle in degrees out of it?
Thanks!

Posted 10 May 2005 - 02:00 AM

two points are connected with a straight line, there is no angle

Posted 10 May 2005 - 02:07 AM

FAQ - Look up the atan2() function in your C documentation.

Posted 10 May 2005 - 02:09 AM

If it's the angle between the two vectors from the origin pointing to p1 and p2, respectively, you can get the angle using the atan() function in the cmath library. atan2 would be even better.

Posted 10 May 2005 - 02:24 AM

Your question is not well expressed. Two points do not have an "angle from one to another".

If you want the the angle between the line defined by these two points and the horizontal axis:

double angle = atan2(y2 - y1, x2 - x1) * 180 / PI;

If you want the angle bewteen the vectors OP1 and OP2 (O being the origin), you should know that the dot product between two vectors u and v is:

u . v = u.x * v.x + u.y * v.y = |u|*|v|*cos(a)

a being the angle between the vectors.

So the angle is given by:

double n1 = sqrt(x1*x1+y1*y1), n2 = sqrt(x2*x2+y2*y2);

double angle = acos((x1*x2+y1*y2)/(n1*n2)) * 180 / PI;

If you want another "angle", well... I can't think of any other!

jods

If you want the the angle between the line defined by these two points and the horizontal axis:

double angle = atan2(y2 - y1, x2 - x1) * 180 / PI;

If you want the angle bewteen the vectors OP1 and OP2 (O being the origin), you should know that the dot product between two vectors u and v is:

u . v = u.x * v.x + u.y * v.y = |u|*|v|*cos(a)

a being the angle between the vectors.

So the angle is given by:

double n1 = sqrt(x1*x1+y1*y1), n2 = sqrt(x2*x2+y2*y2);

double angle = acos((x1*x2+y1*y2)/(n1*n2)) * 180 / PI;

If you want another "angle", well... I can't think of any other!

jods