使用余弦定律计算目标 C 中两点之间的距离? [英] Using the Cosine law to calculate distance between 2 points in Objective C?

查看:35
本文介绍了使用余弦定律计算目标 C 中两点之间的距离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从谷歌地图获得了一些 GPS 坐标,我需要找到它们之间的距离使用目标 C.我已经实现了公式,但我得到的结果很大.

I get some GPS coordinates from Google Maps and I need to find the distance between them using Objective C. I have implemented the formula but I get results that are way to big.

我已经通过将 Google 地图中的值传回 Google 地球和互联网上的地理编码服务来测试这些值,并且一切都经过检查.我现在开始怀疑余弦定律要求我在传递坐标之前对坐标进行某种转换.

I have tested the values from Google Maps by passing them back into Google Earth and a Geocoding service on the internet and everything checks out. Im now beginning to suspect that the cosine law demands I do some sort of conversion with the coordinates before I pass them in.

我对Haversine 公式进行了类似的实现,但这也给了我很大的成果.然后我切换到余弦,因为它更容易调试,而且我不需要非常高的精度.

I did a similar implementation of the Haversine formula, but this also gave me to big results. I then switched to the cosine since it was easier to debug and I don't need very high precision.

希望有人能对此有所了解,或使用代码:)

Hope someone could shed a little light on this one, or use the code:)

- (CGFloat) calculateDistanceBetweenPoints:(CGPoint) origin andDestination:(CGPoint) destination {

//To convert kilometers to miles, divide by 1.609
// x = latitude 
// y = longitude

/* example:
 Dubai      : 25.248665, 55.352917 
 Amsterdam  : 52.309071, 4.763385
 Approx dist: 5,182.62 KM
 Calc. dist : 8,253.33
 */

CGFloat toRad           =   (M_PI / 180);
CGFloat R               =   6371.0f; //earth's mean radius in Km

CGFloat sinePart        =   sinf( origin.x * toRad ) * sinf( destination.x * toRad );
CGFloat cosinePart      =   cosf( origin.x * toRad ) * cosf( destination.x * toRad );
CGFloat deltaCosinePart =   cosf( ( destination.y - origin.y ) * toRad );

CGFloat delta           =   acosf( sinePart + cosinePart * deltaCosinePart) * R;

return delta;
}

以上根据此处引用的链接计算:stackoverflow 问题

Above calculated from links referenced here:stackoverflow question

推荐答案

代码可能没问题,当我在您的示例数据(减去几个小数位)上运行它时,它返回 5168.3584

The code might be alright, when i run it on your example data (minus a couple of decimal places) it returns 5168.3584

这篇关于使用余弦定律计算目标 C 中两点之间的距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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