Skip to content

Instantly share code, notes, and snippets.

@anchietajunior
Created May 23, 2020 20:03
Show Gist options
  • Save anchietajunior/eca12b1a181fb4c28236b93ef26daf55 to your computer and use it in GitHub Desktop.
Save anchietajunior/eca12b1a181fb4c28236b93ef26daf55 to your computer and use it in GitHub Desktop.
Implementation
def distance_between_locations(locations)
def distance(location_one, location_two)
rad_per_deg = Math::PI/180
radius_in_meters = 6371 * 1000
dlat_rad = (location_two[0]-location_one[0]) * rad_per_deg
dlon_rad = (location_two[1]-location_one[1]) * rad_per_deg
latitude_one_rad, lon1_rad = location_one.map {|i| i * rad_per_deg }
latitude_two_rad, lon2_rad = location_two.map {|i| i * rad_per_deg }
a = Math.sin(dlat_rad/2)**2 + Math.cos(latitude_one_rad) * Math.cos(latitude_two_rad) * Math.sin(dlon_rad/2)**2
c = 2 * Math::atan2(Math::sqrt(a), Math::sqrt(1-a))
radius_in_meters * c
end
locations.each_with_index do |initial_point, initial_index|
p "Distance from the point #{initial_index + 1} to:"
locations.each_with_index do |point, index|
if index != initial_index
p "Point #{index + 1}: #{distance(initial_point, point).to_i} meters."
end
end
end
end
# Londrina-PR, Paulo Afonso-BA, Aracaju-SE
locations = [
[-23.3045386, -51.1689972],
[-9.5368384, -38.3955786],
[-11.0059998, -37.1731953]
]
distance_between_locations(locations)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment