diff --git a/src/haversine/distance.cr b/src/haversine/distance.cr index 4a5c49f..27f868c 100644 --- a/src/haversine/distance.cr +++ b/src/haversine/distance.cr @@ -4,12 +4,25 @@ module Haversine EARTH_RADIUS = 6371008.8 + # Unit of measurement factors using a spherical (non-ellipsoid) earth radius. + # + # Keys are the name of the unit, values are the number of that unit in a single radians FACTORS = { + centimeters: EARTH_RADIUS * 100, + centimetres: EARTH_RADIUS * 100, + degrees: 360 / (2 * Math::PI), + feet: EARTH_RADIUS * 3.28084, + inches: EARTH_RADIUS * 39.37, kilometers: EARTH_RADIUS / 1000, + kilometres: EARTH_RADIUS / 1000, meters: EARTH_RADIUS, + metres: EARTH_RADIUS, miles: EARTH_RADIUS / 1609.344, - feet: EARTH_RADIUS * 3.28084, + millimeters: EARTH_RADIUS * 1000, + millimetres: EARTH_RADIUS * 1000, nautical_miles: EARTH_RADIUS / 1852, + radians: 1, + yards: EARTH_RADIUS * 1.0936, } property distance @@ -17,7 +30,7 @@ module Haversine def initialize(@distance : Number) end - {% for factor in ["meters", "kilometers", "miles", "nautical_miles", "feet"] %} + {% for factor in FACTORS.keys %} def to_{{factor.id}} : Number @distance * FACTORS[:{{factor.id}}] end