Skip to content

Instantly share code, notes, and snippets.

@nickbauman
Created July 26, 2020 01:21
Show Gist options
  • Save nickbauman/79a45ad07b59d689ed3d979131c2b0cc to your computer and use it in GitHub Desktop.
Save nickbauman/79a45ad07b59d689ed3d979131c2b0cc to your computer and use it in GitHub Desktop.
(ns haversine
(:require [math.numeric-tower :refer [expt sqrt]]))
(def RADIUS_OF_EARTH 6371.0088)
(defn radians [degrees]
(/ (* degrees Math/PI) 180))
(defn haversine
[lat1 lon1 lat2 lon2]
(let [[lon1 lat1 lon2 lat2] (map radians [lon1 lat1 lon2 lat2])
lng (- lon2 lon1)
lat (- lat2 lat1)
a (+ (expt (Math/sin (* lat 1/2)) 2)
(* (Math/cos lat1)
(Math/cos lat2)
(expt (Math/sin (/ lng 2)) 2)))
c (* 2 (Math/asin (sqrt a)))]
(* 1000 (* c RADIUS_OF_EARTH)))) ; meters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment