Class: Geodesy::Destination
- Inherits:
-
Object
- Object
- Geodesy::Destination
- Defined in:
- lib/geodesy/destination.rb
Instance Attribute Summary collapse
-
#bearing ⇒ Object
readonly
Returns the value of attribute bearing.
-
#distance ⇒ Object
readonly
Returns the value of attribute distance.
-
#radius ⇒ Object
readonly
Returns the value of attribute radius.
-
#starting_point ⇒ Object
readonly
Returns the value of attribute starting_point.
Instance Method Summary collapse
- #calculate ⇒ Object
-
#initialize(starting_point, bearing, distance) ⇒ Destination
constructor
StartingPoint is a Coordinate Object Bearing is a Float Object Distance is an Integer or Float Object - default units: meters.
Constructor Details
#initialize(starting_point, bearing, distance) ⇒ Destination
StartingPoint is a Coordinate Object Bearing is a Float Object Distance is an Integer or Float Object - default units: meters
16 17 18 19 20 21 |
# File 'lib/geodesy/destination.rb', line 16 def initialize(starting_point, bearing, distance) @starting_point = starting_point @radius = Geodesy::EARTH_RADIUS @bearing = bearing @distance = distance end |
Instance Attribute Details
#bearing ⇒ Object (readonly)
Returns the value of attribute bearing.
9 10 11 |
# File 'lib/geodesy/destination.rb', line 9 def bearing @bearing end |
#distance ⇒ Object (readonly)
Returns the value of attribute distance.
9 10 11 |
# File 'lib/geodesy/destination.rb', line 9 def distance @distance end |
#radius ⇒ Object (readonly)
Returns the value of attribute radius.
9 10 11 |
# File 'lib/geodesy/destination.rb', line 9 def radius @radius end |
#starting_point ⇒ Object (readonly)
Returns the value of attribute starting_point.
9 10 11 |
# File 'lib/geodesy/destination.rb', line 9 def starting_point @starting_point end |
Instance Method Details
#calculate ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/geodesy/destination.rb', line 23 def calculate angular_distance = distance / radius bearing_to_radians = bearing.to_radians lat_radians = starting_point.lat.to_radians lng_radians = starting_point.lng.to_radians x = Math.asin( ( Math.sin(lat_radians) * Math.cos(angular_distance) ) + ( Math.cos(lat_radians) * Math.sin(angular_distance) * Math.cos(bearing_to_radians) ) ) y = lng_radians + Math.atan2( Math.sin(bearing_to_radians) * Math.sin(angular_distance) * Math.cos(lat_radians), Math.cos(angular_distance) - Math.sin(lat_radians) * Math.sin(x) ) # normalize y to -180°..+180° z = ( y + 3 * Math::PI ) % ( 2 * Math::PI ) - Math::PI Coordinates.new( x.to_degrees, z.to_degrees ) end |