开发者

sperical law of cosines formula

I'm tring to calculate a point's longitude from the other point's longitude. They have the same latitude and the distance between them is known. I try to use the sperical law of cosines formula.

# 'lat' short for 'latitude', 'lng' short for 'longitude'.
# EARTH_RADIUS = 6371000.0, unit is meter.
#
distance = Math.acos( Math.sin(lat1)*Math.sin(lat2) + 
           Math.cos(lat1)*Math.cos(lat2) * 
           Math.cos(lng2-lng1)) * EARTH_RADIUS

开发者_JS百科If the two point's latitude are equal(lat1 == lat2), i can calculate lng2 from lng1 with distance. So i reason the formula from sperical law of cosines formula

# lat1 == lat2 == lat
# 'distance' and 'lng' are known 
lng2 = Math.acos((Math.cos(distance/EARTH_RADIUS) - Math.sin(lat)*Math.sin(lat))/(Math.cos(lat)*Math.cos(lat))) + lng

This formula works very well, except some situations.

Like

lat_degrees =  -89.8345981836319  
lng_degrees = 96.42309331893921
lat = lat1 = lat2 = (lat_degrees * Math::PI)/180 # -1.567909520510494
lng = (lng_degrees * Math::PI)/180  # 1.682900453373236 
distance = 67544.06725769254

This will apper the error

Math::DomainError: Numerical argument is out of domain - "acos"

Because the value in Math.acos(value) equal to -2.5100189069914602, which smaller than -1. I have no idea about that. Is the derived formual wrong?


There is nothing wrong with your formula. I didn't do the calculations but I suppose the point is that you are (very!!) near to the south pole and there basically are no two points which lie apart such large distance.


just add a compare before it:

if lat1 == lat2 and lng1 == lng2
  return 0
end
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜