确定平均角度 [英] Determining the average angle

查看:77
本文介绍了确定平均角度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个涉及在游戏中获取摄影机角度的应用程序.角度可以是0-359之间的任何值. 0是北,90是东,180是南,等等.我使用的是API,该类在Camera类中具有getAngle()方法.

I'm developing an application that involves getting the camera angle in a game. The angle can be anywhere from 0-359. 0 is North, 90 is East, 180 is South, etc. I'm using an API, which has a getAngle() method in Camera class.

我如何找到不同相机角度之间的平均值. 0和359的真实平均值是179.5.作为摄影机角度,应该是南,但是显然0和359都非常接近北.

How would I find the average between different camera angles. The real average of 0 and 359 is 179.5. As a camera angle, that would be South, but obviously 0 and 359 are both very close to North.

推荐答案

您可以从向量的角度来考虑它.假设θ1θ2是您用弧度表示的两个角度.然后我们可以确定在这些角度的单位矢量的x和y分量:

You can think of it in terms of vectors. Let θ1 and θ2 be your two angles expressed in radians. Then we can determine the x and y components of the unit vectors that are at these angles:


x1 = sin(θ1)
y1 = cos(θ1)

x2 = sin(θ2)
y2 = cos(θ2)

然后可以将这两个向量相加,并确定结果的x和y分量:

You can then add these two vectors, and determine the x and y components of the result:


x* = x1 + x2
y* = y1 + y2

最后,您可以确定此结果矢量的角度:

Finally, you can determine the angle of this resulting vector:


θavg = tan-1(y*/x*)

,或者甚至更好,请使用atan2(许多语言支持的功能):

or, even better, use atan2 (a function supported by many languages):


θavg = atan2(y*, x*)

您可能必须分别处理y* = 0 x* = 0的情况,因为这意味着两个向量指向的方向完全相反(所以平均值"应该是多少? ).

You will probably have to separately handle the cases where y* = 0 and x* = 0, since this means the two vectors are pointing in exactly opposite directions (so what should the 'average' be?).

这篇关于确定平均角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆