From 0923354b9b0b384a8aea77b15a99f0ba4c5b756d Mon Sep 17 00:00:00 2001 From: Anton Maminov Date: Wed, 3 Apr 2024 12:30:21 +0300 Subject: [PATCH 1/2] set Earth radius to 6371008.8 --- spec/haversine_spec.cr | 16 ++++++++-------- src/haversine/distance.cr | 30 +++++++++++++++++------------- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/spec/haversine_spec.cr b/spec/haversine_spec.cr index 04c0142..8313b02 100644 --- a/spec/haversine_spec.cr +++ b/spec/haversine_spec.cr @@ -13,9 +13,9 @@ describe Haversine do end describe "calculates the distance between the provided lat/lon pairs" do - it { Haversine.distance(0, 0, 0, 0).great_circle_distance.should eq(0) } - it { Haversine.distance([0.0, 0.0], [0.0, 0.0]).great_circle_distance.should eq(0) } - it { Haversine.distance({0.0, 0.0}, {0.0, 0.0}).great_circle_distance.should eq(0) } + it { Haversine.distance(0, 0, 0, 0).distance.should eq(0) } + it { Haversine.distance([0.0, 0.0], [0.0, 0.0]).distance.should eq(0) } + it { Haversine.distance({0.0, 0.0}, {0.0, 0.0}).distance.should eq(0) } end describe "computes distances correctly" do @@ -24,11 +24,11 @@ describe Haversine do dist = Haversine.distance(new_york, london) - it { dist.to_kilometers.should eq(5570.4744596620685) } - it { dist.to_meters.should eq(5570474.459662069) } - it { dist.to_miles.should eq(3458.9227691764468) } - it { dist.to_nautical_miles.should eq(3005.720267276497) } - it { dist.to_feet.should eq(18263112.22125164) } + it { dist.to_kilometers.should eq(5570.482153929098) } + it { dist.to_meters.should eq(5570482.153929098) } + it { dist.to_miles.should eq(3461.3371373237155) } + it { dist.to_nautical_miles.should eq(3007.8197375427094) } + it { dist.to_feet.should eq(18275860.669896744) } end end end diff --git a/src/haversine/distance.cr b/src/haversine/distance.cr index e60d7df..caa84e1 100644 --- a/src/haversine/distance.cr +++ b/src/haversine/distance.cr @@ -2,39 +2,43 @@ module Haversine class Distance include Comparable(self) - GREAT_CIRCLE_RADIUS_KILOMETERS = 6371 - GREAT_CIRCLE_RADIUS_METERS = GREAT_CIRCLE_RADIUS_KILOMETERS * 1000 - GREAT_CIRCLE_RADIUS_MILES = 3956 - GREAT_CIRCLE_RADIUS_FEET = GREAT_CIRCLE_RADIUS_MILES * 5280 - GREAT_CIRCLE_RADIUS_NAUTICAL_MILES = GREAT_CIRCLE_RADIUS_MILES / 1.15078 + EARTH_RADIUS = 6371008.8 - property great_circle_distance + FACTORS = { + kilometers: EARTH_RADIUS / 1000, + meters: EARTH_RADIUS, + miles: EARTH_RADIUS / 1609.344, + feet: EARTH_RADIUS * 3.28084, + nauticalmiles: EARTH_RADIUS / 1852, + } - def initialize(@great_circle_distance : Number) + property distance + + def initialize(@distance : Number) end def to_kilometers : Number - @great_circle_distance * GREAT_CIRCLE_RADIUS_KILOMETERS + @distance * FACTORS[:kilometers] end def to_meters : Number - @great_circle_distance * GREAT_CIRCLE_RADIUS_METERS + @distance * FACTORS[:meters] end def to_miles : Number - @great_circle_distance * GREAT_CIRCLE_RADIUS_MILES + @distance * FACTORS[:miles] end def to_nautical_miles : Number - @great_circle_distance * GREAT_CIRCLE_RADIUS_NAUTICAL_MILES + @distance * FACTORS[:nauticalmiles] end def to_feet : Number - @great_circle_distance * GREAT_CIRCLE_RADIUS_FEET + @distance * FACTORS[:feet] end def <=>(other : Haversine::Distance) - great_circle_distance <=> other.great_circle_distance + distance <=> other.distance end end end From f91cbb529aadb958e9fa195efd87a93510526109 Mon Sep 17 00:00:00 2001 From: Anton Maminov Date: Wed, 3 Apr 2024 12:36:47 +0300 Subject: [PATCH 2/2] refactor code --- src/haversine/distance.cr | 34 ++++++++++------------------------ 1 file changed, 10 insertions(+), 24 deletions(-) diff --git a/src/haversine/distance.cr b/src/haversine/distance.cr index caa84e1..4a5c49f 100644 --- a/src/haversine/distance.cr +++ b/src/haversine/distance.cr @@ -5,11 +5,11 @@ module Haversine EARTH_RADIUS = 6371008.8 FACTORS = { - kilometers: EARTH_RADIUS / 1000, - meters: EARTH_RADIUS, - miles: EARTH_RADIUS / 1609.344, - feet: EARTH_RADIUS * 3.28084, - nauticalmiles: EARTH_RADIUS / 1852, + kilometers: EARTH_RADIUS / 1000, + meters: EARTH_RADIUS, + miles: EARTH_RADIUS / 1609.344, + feet: EARTH_RADIUS * 3.28084, + nautical_miles: EARTH_RADIUS / 1852, } property distance @@ -17,25 +17,11 @@ module Haversine def initialize(@distance : Number) end - def to_kilometers : Number - @distance * FACTORS[:kilometers] - end - - def to_meters : Number - @distance * FACTORS[:meters] - end - - def to_miles : Number - @distance * FACTORS[:miles] - end - - def to_nautical_miles : Number - @distance * FACTORS[:nauticalmiles] - end - - def to_feet : Number - @distance * FACTORS[:feet] - end + {% for factor in ["meters", "kilometers", "miles", "nautical_miles", "feet"] %} + def to_{{factor.id}} : Number + @distance * FACTORS[:{{factor.id}}] + end + {% end %} def <=>(other : Haversine::Distance) distance <=> other.distance