开发者

Calculating distance between two points using pythagorean theorem [closed]

Closed. This question is off-topic. It is not currently accepting answers.
开发者_如何学Python

Want to improve this question? Update the question so it's on-topic for Stack Overflow.

Closed 13 years ago.

Improve this question

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. :)

Thanks.

EDIT:

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.

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

Horizontal (EW) distance is roughly

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

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

Vertical (NS) distance is easier

d_ns = (lat1 - lat0)

So the distance between the two points is

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.

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


Well, since your points are near each other, the surface of the sphere is almost flat, so just find the coordinates of the points in 3D space, so find (x,y,z) for each of the points, where

x = r*sin(lat)*cos(long)
y = r*sin(lat)*sin(long)
z = r*cos(lat)

where r is the radius of the sphere. or something like that depending on how you define lat/long. Once you have the two xyz coords, just use sqrt((x1-x2)^2+(y1-y2)^2+(z1-z2)^2). You really can't just use a 2D Pythagorean theoreom since you would need to get reasonable 2D coordinates, which is hard.


You will commonly see this notation 'dy, dx' which stands for difference y and difference x. You simply work out the differences on both axises, the get the square root of both differences squared as per the theorum.(the sum of the hype is equal to the square of the other two sides).

var dx:Number = x1-x2;
var dy:Number = y1-y2;
var distance:Number = Math.sqrt(dx*dx + dy*dy);

Hope this is clear enough

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜