Skip to content

Commit

Permalink
refactor distance method
Browse files Browse the repository at this point in the history
  • Loading branch information
mamantoha committed Apr 10, 2024
1 parent 9f18e7c commit 39e8719
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/haversine.cr
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,15 @@ module Haversine

# Calculates the haversine distance between two locations using latitude and longitude.
def distance(lat1 : Number, lon1 : Number, lat2 : Number, lon2 : Number) : Haversine::Distance
dlon = lon2 - lon1
dlat = lat2 - lat1
dlon = to_radians(lon2 - lon1)
dlat = to_radians(lat2 - lat1)
lat1 = to_radians(lat1)
lat2 = to_radians(lat2)

a =
Math.sin(dlat / 2) ** 2 +
(Math.sin(dlon / 2) ** 2) * Math.cos(lat1) * Math.cos(lat2)

a = calc(dlat, lat1, lat2, dlon)
c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))

Haversine::Distance.new(c)
Expand Down Expand Up @@ -99,11 +104,6 @@ module Haversine
destination(latitude, longitude, distance, bearing, unit)
end

private def calc(dlat : Number, lat1 : Number, lat2 : Number, dlon : Number) : Number
Math.sin(to_radians(dlat) / 2) ** 2 +
Math.cos(to_radians(lat1)) * Math.cos(to_radians(lat2)) * (Math.sin(to_radians(dlon) / 2)) ** 2
end

private def to_radians(degrees : Number) : Number
degrees * Math::PI / 180.0
end
Expand Down

0 comments on commit 39e8719

Please sign in to comment.