使用勾股定理计算两点之间的距离 [英] Calculating distance between two points using pythagorean theorem

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

问题描述

我想创建一个函数,该函数使用pythag定理而不是Haversine大圆公式来计算两对经纬度之间的距离.由于这将是相对较短的距离(3公里),因此我认为此版本假定平坦的地面应该可以.我怎样才能做到这一点?我询问了互联网,但没有提出任何有用的建议. :)

I'd like to create a function that calculates the distance between two pairs of lat/longs using the pythag theorem instead of the haversine great-circle formula. Since this will be over relative short distances (3km), I think this version that assumes a flat earth should be OK. How can I do this? I asked the internet and didn't come up with anything useful. :)

谢谢.

这是我想出的(似乎正在起作用):

Here's what I came up with (seems to be working):

def get_dist(lat0, lng0, lat1, lng1)
  begin
    d_ew = (lng1.to_f - lng0.to_f) * Math.cos(lat0.to_f)
    d_ns = (lat1.to_f - lat0.to_f)
    d_lu = Math.sqrt(d_ew.to_f * d_ew.to_f + d_ns.to_f * d_ns.to_f)
    d_mi = ((2*Math::PI*3961.3)/360)*d_lu
    return d_mi
  rescue Exception => ex
    logger.debug "[get_dist] An exception occurred: #{ex.message}"
    return -1
  end
end

推荐答案

如果您期望所涉及的距离与地球的大小相比较小,则可以使用一个简单的毕达哥拉斯三角形.

You can use a simple pythagoras triangle if you expect the distances involved to be small compared with the size of the Earth.

假设您位于(lat0,long0),并且想以纬度单位"知道到点(lat1,long1)的距离.

Suppose you are at (lat0, long0) and you want to know the distance to a point (lat1, long1) in "latitude units".

水平(EW)距离大约

d_ew = (long1 - long0) * cos(lat0)

乘以cos(lat0)可以算出在高纬度时经线越来越靠近.

This is multiplied by cos(lat0) to account for longitude lines getting closer together at high latitude.

垂直(NS)距离更容易

Vertical (NS) distance is easier

d_ns = (lat1 - lat0)

所以两点之间的距离是

d = sqrt(d_ew * d_ew + d_ns * d_ns)

您可以改进此方法来执行更严格的任务,但这对于比较距离应该足够好了.

You can refine this method for more exacting tasks, but this should be good enough for comparing distances.

实际上,对于比较距离,可以比较d平方,这意味着您可以省略sqrt运算.

In fact, for comparing distances, it will be fine to compare d squared, which means you can omit the sqrt operation.

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

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