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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With