Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
670 views
in Technique[技术] by (71.8m points)

.net - Averaging angles

I looked at some solutions here but none provide what I need, so:

I need to average an array of angles(0 to 359.9, no negatives) (A1 + A2 + A3 + An) / n

The issue is when you get an array {1, 359, 2, 358} the average if you use the formula above is 180, but actually it is supposed to be 0.

Any thoughts?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add unit vectors of each angle, and convert the resulting vector back into an angle. If the result vector is of zero length, the inputs cancelled each other out and the result is indeterminate.

A unit vector has a length of 1, and its x and y lengths are given by the cosine and sine of the angle. Thus you average your examples as in the following pseudo-code:

x = cos(radians(1)) + cos(radians(359)) + cos(radians(2)) + cos(radians(358));
y = sin(radians(1)) + sin(radians(359)) + sin(radians(2)) + sin(radians(358));
angle = degrees(atan2(y, x));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...