quote: | Originally posted by Joss Weatherby
I need to get the average of a set of angles...
angles = [355, 358, 0, 5, 351];
So I basically convert those numbers into radians.
Then I go through the radians and I convert them to their sine and cosine values and add those up.
I then get the average (totalSine/5) and (totalCosine/5)...
I then use atan2 to convert the averages back into an angle...
It doesn't work. I have mixed and matched values all over the place...
I get weird ass fucking numbers, like I give it two or three of the same number and I get back like 6.124125 or some really odd number.
I looked at this post: http://www.bio.net/bionet/mm/molmod...ary/000853.html
and...
this one I started off with and got those numbers to test: http://positivelyglorious.com/softw...es-of-azimuths/
Any ideas? |
You should not average the sine and cosine values. Just sum all sines, sum all cosines. Calculate sum(sine)/sum(cos) and then take inverse tan of the number. Convert it back to degrees, and you will get about -2.2. Don't ignore zero. Cosine(0) = 1 !
quote: | Originally posted by Joss Weatherby
all this -5 stuff is great but where do i start making them negatives, this is an arbitrary list of values, not something i have defined.
Winston I tried that but I keep getting fucked up values, should they be radians when getting there sine and cosine values? |
Because the list is arbitrary, you need to use the sine and cosine method. That takes care of the arbitrariness. And yes, they should all be in radians. By using sines and cosines you are using vectors to solve the problem.
So you represent 355 (in rad) as i*cos(355*pi/180) + j*sin(355*pi/180), where i and j are unit vectors along 2 perpendicular lines X and Y, forming a plane, where you are measuring the angles from the X axis in the counter clockwise direction. You represent all numbers in this way. Then you add up all cosines, you add up all the sines, and you end up with a vector, i*A + j*B, where A, B are the sums of the cosine and sine terms. The angle of this vector is ATAN(B/A). Multiply by 180/pi and convert it back to degrees. This is your average angle.
___________________

Download and review ! Omega_M - In the Mix (Beta Version)
Originally posted by twilightki : It feels like something you'd listen to at 4 in the morning, or listen to in your car while you're going in a tunnel.
|