user2336450
user2336450

Reputation: 47

C++ angles, trigonometry

I need some help calculating angles of points: enter image description here

I need to calculate angle from point (0,0) to points extracted from image. 1 would be 0*, 2 is about 40-44* etc.

My problem is that atan2 shows incorrect values. Current output of atan2 is:

 1:41.867535 2:64.653824 3:52.915009 4:30.375608 5:13.328092 

How can I calculate it from point 0,0? I can't use any non-standard libraries.

I'm still doing something wrong. I'm trying:

   arrow1 = (M_PI - atan2(y, x) * (180 / M_PI);

Output:

   1: 131.867538  2: 154.653824  3: 142.915009  4: 120.375610  5: 103.328094 

And:

   arrow1 = (M_PI - atan2(y, -x) * (180 / M_PI); 

Output:

    1: 48.132465  2: 25.346176  3: 37.084991  4: 59.624393  5: 76.671906 

Upvotes: 1

Views: 555

Answers (1)

Reed Copsey
Reed Copsey

Reputation: 564323

The angle returned from atan2(deltaY, deltaX) will be the angle, in radians, counter clockwise from the X axis.

You are currently using arrow1 = atan2(x,y) *180 / M_PI;, so you need to convert that to using (y,x), then also switch so you're taking the angle clockwise from -X instead of CCW from +X.

This means the angle for point 1, if you feed it as atan2(-1, 0), will be 180 degrees. To achieve the angle you wish, it should be:

double angleFromX = atan2(deltaY, deltaX);
double angle = M_PI - angleFromX;
double angleInDegrees = 180 * angle / M_PI;

Upvotes: 4

Related Questions