Class: GpsUtils::Point
- Inherits:
-
Object
- Object
- GpsUtils::Point
- Defined in:
- lib/gpsutils.rb
Instance Attribute Summary collapse
-
#lat ⇒ Float, Integer
readonly
Latitude.
-
#lng ⇒ Float, Integer
readonly
Longitude.
Instance Method Summary collapse
-
#distance(other) ⇒ Float
Measure the distance between this point and another.
-
#initialize(lat, lng) ⇒ Point
constructor
Initialize Point.
-
#to_a ⇒ Array
Array with latitude and longitude as Float or Integer values, as used when object was initialized.
- #to_s ⇒ Object
Constructor Details
#initialize(lat, lng) ⇒ Point
Initialize Point.
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/gpsutils.rb', line 21 def initialize(lat, lng) unless lat.is_a? Float or lat.is_a? Integer raise ArgumentError.new 'lat must be float or integer.' end unless lng.is_a? Float or lng.is_a? Integer raise ArgumentError.new 'lng must be float or integer.' end @lat = lat @lng = lng end |
Instance Attribute Details
#lat ⇒ Float, Integer (readonly)
Returns Latitude.
11 12 13 |
# File 'lib/gpsutils.rb', line 11 def lat @lat end |
#lng ⇒ Float, Integer (readonly)
Returns Longitude.
15 16 17 |
# File 'lib/gpsutils.rb', line 15 def lng @lng end |
Instance Method Details
#distance(other) ⇒ Float
Measure the distance between this point and another.
Distance is calculated using equirectangular projection.
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/gpsutils.rb', line 52 def distance(other) unless other.is_a? Point raise ArgumentError.new 'other must be a Point.' end dlng = GpsUtils::to_radians(other.lng - @lng) dlat = GpsUtils::to_radians(other.lat - @lat) x = dlng * Math.cos(dlat / 2) y = GpsUtils::to_radians(other.lat - @lat) Math.sqrt(x**2 + y**2) * GpsUtils::R end |
#to_a ⇒ Array
Returns Array with latitude and longitude as Float or Integer values, as used when object was initialized.
36 37 38 |
# File 'lib/gpsutils.rb', line 36 def to_a [@lat, @lng] end |
#to_s ⇒ Object
40 41 42 |
# File 'lib/gpsutils.rb', line 40 def to_s "#{@lat},#{@lng}" end |