From 017338648b8ca3f361aa7a1e53ace7efa5e8f221 Mon Sep 17 00:00:00 2001 From: Anton Maminov Date: Fri, 29 Mar 2024 14:19:11 +0200 Subject: [PATCH] cosmetic changes --- spec/geo_bearing_spec.cr | 4 ++++ src/geo_bearing.cr | 10 ++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/spec/geo_bearing_spec.cr b/spec/geo_bearing_spec.cr index ed04ebe..7036857 100644 --- a/spec/geo_bearing_spec.cr +++ b/spec/geo_bearing_spec.cr @@ -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 diff --git a/src/geo_bearing.cr b/src/geo_bearing.cr index d9084be..cdb7ff0 100644 --- a/src/geo_bearing.cr +++ b/src/geo_bearing.cr @@ -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) @@ -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) @@ -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