半正弦公式 Unity [英] Haversine formula Unity

查看:32
本文介绍了半正弦公式 Unity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图在 Unity 中使用半正弦公式来获得两个不同点(给定的纬度和经度)之间的距离.代码正在运行(没有错误),但我一直得到错误的结果.我遵循了整个公式,所以我真的不知道数学/代码问题在哪里.有什么想法吗?

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?

代码如下:

 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;
 }

推荐答案

我认为这一行不正确:

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

更新:

正确的做法是:

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

这篇关于半正弦公式 Unity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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