Skip to content

Commit

Permalink
cosmetic changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mamantoha committed Mar 29, 2024
1 parent 6229514 commit 0173386
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
4 changes: 4 additions & 0 deletions spec/geo_bearing_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ describe Geo::Bearing do
lat1, lng1 = {48.8566, 2.3522} # Paris
lat2, lng2 = {40.7128, -74.0060} # New York

context "version" do
it { Geo::Bearing::VERSION.should be_a(String) }
end

context "bearing" do
# Checked on https://www.movable-type.co.uk/scripts/latlong.html
context "calculates initial bearing" do
Expand Down
10 changes: 6 additions & 4 deletions src/geo_bearing.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ module Geo
module Bearing
VERSION = {{ `shards version #{__DIR__}`.chomp.stringify }}

extend self

# The formula used for calculating the initial bearing between two points on the Earth's surfaceis
# is derived from the broader concepts of great-circle distance and navigation on a spherical Earth model.
# https://en.wikipedia.org/wiki/Great-circle_distance
#
# https://github.com/Turfjs/turf/blob/master/packages/turf-bearing/index.ts
def self.bearing(from_lat : Number, from_lng : Number, to_lat : Number, to_lng : Number, final = false) : Float64
def bearing(from_lat : Number, from_lng : Number, to_lat : Number, to_lng : Number, final = false) : Float64
if final
# Calculate the bearing from the destination point back to the original point
reverse_bearing = calculate_bearing(to_lat, to_lng, from_lat, from_lng)
Expand All @@ -19,7 +21,7 @@ module Geo
end
end

private def self.calculate_bearing(lat1, lng1, lat2, lng2) : Float64
private def calculate_bearing(lat1, lng1, lat2, lng2) : Float64
rad_lat1 = degrees_to_radians(lat1)
rad_lng1 = degrees_to_radians(lng1)
rad_lat2 = degrees_to_radians(lat2)
Expand All @@ -34,11 +36,11 @@ module Geo
radians_to_degrees(Math.atan2(a, b)) % 360
end

def self.degrees_to_radians(degrees : Number) : Float64
def degrees_to_radians(degrees : Number) : Float64
degrees * Math::PI / 180.0
end

def self.radians_to_degrees(radians : Number) : Float64
def radians_to_degrees(radians : Number) : Float64
radians * 180.0 / Math::PI
end
end
Expand Down

0 comments on commit 0173386

Please sign in to comment.