Haversine formula Unity

徘徊边缘 提交于 2021-02-07 14:14:22

问题


So I'm trying to use the haversine formula in Unity to get the distance between two different points (latitude and longitud given). The code is working (no errors) but I keep gettting a wrong result. I followed the entire formula so I don't really know where the math/code problem is. Any idea?

Here's the code:

 public float lat1 = 42.239616f;
 public float lat2 = -8.72304f;
 public float lon1 = 42.239659f;
 public float lon2 = -8.722305f;

 void operacion(){
 float R = 6371000; // metres
 float omega1 = ((lat1/180)*Mathf.PI);
 float omega2 = ((lat2/180)*Mathf.PI);
 float variacionomega1 = (((lat2 - lat1)/180)*Mathf.PI);
 float variacionomega2 = (((lon2 - lon1) / 180) * Mathf.PI);
 float a = Mathf.Sin(variacionomega1/2) * Mathf.Sin(variacionomega1/2) +
             Mathf.Cos(omega1) * Mathf.Cos(omega2) *
             Mathf.Sin(variacionomega2/2) * Mathf.Sin(variacionomega2/2);
 float c = 2 * Mathf.Atan2(Mathf.Sqrt(a), Mathf.Sqrt(1-a));

 float d = R * c;
 }

回答1:


I think this line is incorrect:

float c = 2 * Mathf.Atan2(Mathf.Sqrt(a), Mathf.Sqrt(1-a));

UPDATED:

The correct way would be:

float c = 2 * Mathf.Asin(Mathf.Sqrt(a));


来源:https://stackoverflow.com/questions/39111063/haversine-formula-unity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!