Class: TspRunner::Location
- Inherits:
-
Object
- Object
- TspRunner::Location
- Defined in:
- lib/tsp_runner/location.rb
Constant Summary collapse
- RADIANS_PER_DEGREE =
Math::PI / 180
- EARTH_RADIUS =
in meters
6371 * 1000
Instance Attribute Summary collapse
-
#lat ⇒ Object
readonly
Returns the value of attribute lat.
-
#lon ⇒ Object
readonly
Returns the value of attribute lon.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #distance_from(location) ⇒ Fixnum
-
#initialize(name, lat, lon) ⇒ Location
constructor
A new instance of Location.
-
#lat_rad ⇒ Fixnum
The latitude in radians.
-
#lon_rad ⇒ Fixnum
The longitude in radians.
Constructor Details
#initialize(name, lat, lon) ⇒ Location
Returns a new instance of Location.
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/tsp_runner/location.rb', line 11 def initialize(name, lat, lon) unless lat.in?(-90..90) raise ArgumentError, 'Latitudue should be between -90 and 90' end unless lon.in?(-180..180) raise ArgumentError, 'Longitudue should be between -180 and 180' end @name, @lat, @lon = name, lat, lon end |
Instance Attribute Details
#lat ⇒ Object (readonly)
Returns the value of attribute lat.
6 7 8 |
# File 'lib/tsp_runner/location.rb', line 6 def lat @lat end |
#lon ⇒ Object (readonly)
Returns the value of attribute lon.
6 7 8 |
# File 'lib/tsp_runner/location.rb', line 6 def lon @lon end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/tsp_runner/location.rb', line 6 def name @name end |
Instance Method Details
#distance_from(location) ⇒ Fixnum
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/tsp_runner/location.rb', line 38 def distance_from(location) delta_lat = location.lat_rad - lat_rad delta_lon = location.lon_rad - lon_rad a = Math.sin(delta_lat / 2) ** 2 + Math.cos(lat_rad) * Math.cos(location.lat_rad) * Math.sin(delta_lon / 2) ** 2 c = 2 * Math::atan2(Math::sqrt(a), Math::sqrt(1 - a)) EARTH_RADIUS * c end |
#lat_rad ⇒ Fixnum
Returns the latitude in radians.
24 25 26 |
# File 'lib/tsp_runner/location.rb', line 24 def lat_rad lat * RADIANS_PER_DEGREE end |
#lon_rad ⇒ Fixnum
Returns the longitude in radians.
29 30 31 |
# File 'lib/tsp_runner/location.rb', line 29 def lon_rad lon * RADIANS_PER_DEGREE end |