Skip to content

Instantly share code, notes, and snippets.

@shayanjm
Created March 23, 2015 17:25
Show Gist options
  • Save shayanjm/451a3242685225aa934b to your computer and use it in GitHub Desktop.
Save shayanjm/451a3242685225aa934b to your computer and use it in GitHub Desktop.
Implementation of the reverse of Haversine formula. Takes one set of latitude/longitude as a start point, a bearing, and a distance, and returns the resultant lat/long pair.
(defn reverse-haversine
"Implementation of the reverse of Haversine formula. Takes one set of latitude/longitude as a start point, a bearing, and a distance, and returns the resultant lat/long pair."
[{lon :long lat :lat bearing :bearing distance :distance}]
(let [R 6378.137 ; Radius of Earth in km
lat1 (Math/toRadians lat)
lon1 (Math/toRadians lon)
angdist (/ distance R)
theta (Math/toRadians bearing)
lat2 (Math/toDegrees (Math/asin (+ (* (Math/sin lat1) (Math/cos angdist)) (* (Math/cos lat1) (Math/sin angdist) (Math/cos theta)))))
lon2 (Math/toDegrees (+ lon1 (Math/atan2 (* (Math/sin theta) (Math/sin angdist) (Math/cos lat1)) (- (Math/cos angdist) (* (Math/sin lat1) (Math/sin lat2))))))]
{:lat lat2 :lon lon2}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment