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..4a5c49f 100644 --- a/src/haversine/distance.cr +++ b/src/haversine/distance.cr @@ -2,39 +2,29 @@ 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, + nautical_miles: EARTH_RADIUS / 1852, + } - def initialize(@great_circle_distance : Number) - end - - def to_kilometers : Number - @great_circle_distance * GREAT_CIRCLE_RADIUS_KILOMETERS - end - - def to_meters : Number - @great_circle_distance * GREAT_CIRCLE_RADIUS_METERS - end - - def to_miles : Number - @great_circle_distance * GREAT_CIRCLE_RADIUS_MILES - end + property distance - def to_nautical_miles : Number - @great_circle_distance * GREAT_CIRCLE_RADIUS_NAUTICAL_MILES + def initialize(@distance : Number) end - def to_feet : Number - @great_circle_distance * GREAT_CIRCLE_RADIUS_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) - great_circle_distance <=> other.great_circle_distance + distance <=> other.distance end end end