quantumpotato
quantumpotato

Reputation: 9767

Average of 2 angles with 360 degrees wrap around

The top answer in Average of two angles with wrap around is wrong per testing + the comments.

The bottom answer 12>

 math:atan(  (math:sin(180)+math:sin(270)) / (math:cos(180)+math:cos(270))).
-1.1946710584651132

but I get -1.946.. instead of the expected 225.

Erlang's math:atan isn't behaving according to http://rapidtables.com/calc/math/Arctan_Calculator.htm , however, which gives different results .

How do I find the average of 2 angles in a circle?

Edit: Attempting to use Radians. Degrees are 180 and 270.

16> A = 180 * 3.14 / 180.
3.14
17> B = 270 * 3.14 / 180.
4.71
18> S = math:sin(A) + math:sin(B).
-0.9984044934712312
19> S2 = S / 2.
-0.4992022467356156
20> C = math:cos(A) + math:cos(B).
-1.002387709839821
21> C2 = C / 2.
-0.5011938549199105
22> math:atan(S, C).
** exception error: undefined function math:atan/2
23> math:atan(S/C).
0.7834073464102068
24> math:atan(S/C) * 180 / 3.14.
44.90870138657236
25> math:atan(S2/C2) * 180 / 3.14.
44.90870138657236

Conversion: -1.19 to degrees = -68.18198.3 360 - 68 = 292. This isn't the expected 225.

Upvotes: 0

Views: 954

Answers (1)

MFisherKDX
MFisherKDX

Reputation: 2866

<cos(t), sin(t)> is the unit vector with angle t in radians. So your formula adds the two unit vectors and and then finds the angle of the resultant vector.

Just use radians instead of degrees and use atan2 instead of atan (which puts the angle on the correct quadrant) and you should see your formula works correctly.

math:atan2( math:sin(t1)+math:sin(t2), math:cos(t1)+math:cos(t2))

Where this formula does not work is if the two angles are precisely 180 degrees apart from each other. In this case the resultant vector is the zero vector and atan is undefined.

Upvotes: 2

Related Questions