Class: Geodesy::Destination

Inherits:
Object
  • Object
show all
Defined in:
lib/geodesy/destination.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#bearingObject (readonly)

Returns the value of attribute bearing.



9
10
11
# File 'lib/geodesy/destination.rb', line 9

def bearing
  @bearing
end

#distanceObject (readonly)

Returns the value of attribute distance.



9
10
11
# File 'lib/geodesy/destination.rb', line 9

def distance
  @distance
end

#radiusObject (readonly)

Returns the value of attribute radius.



9
10
11
# File 'lib/geodesy/destination.rb', line 9

def radius
  @radius
end

#starting_pointObject (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

#calculateObject



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