Skip to content

Instantly share code, notes, and snippets.

@tobyprivett
Created May 13, 2016 20:35
Show Gist options
  • Save tobyprivett/ca105c7e08f40486feb4d7754bc52472 to your computer and use it in GitHub Desktop.
Save tobyprivett/ca105c7e08f40486feb4d7754bc52472 to your computer and use it in GitHub Desktop.
// from https://rosettacode.org/wiki/Haversine_formula#Elixir
defmodule Haversine do
@v :math.pi / 180
@r 6372.8 # km for the earth radius
def distance({lat1, long1}, {lat2, long2}) do
dlat = :math.sin((lat2 - lat1) * @v / 2)
dlong = :math.sin((long2 - long1) * @v / 2)
a = dlat * dlat + dlong * dlong * :math.cos(lat1 * @v) * :math.cos(lat2 * @v)
@r * 2 * :math.asin(:math.sqrt(a))
end
end
bna = {36.12, -86.67}
lax = {33.94, -118.40}
IO.puts Haversine.distance(bna, lax)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment