Skip to content

Commit

Permalink
add more units
Browse files Browse the repository at this point in the history
  • Loading branch information
mamantoha committed Apr 3, 2024
1 parent d088bad commit c194e4a
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/haversine/distance.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,33 @@ 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

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
Expand Down

0 comments on commit c194e4a

Please sign in to comment.