Sha256: f992d7bc7799aab4179b4f55ef85f2f90ceaf1881ef44777fd80c32b8f10b28f

Contents?: true

Size: 1.7 KB

Versions: 6

Compression:

Stored size: 1.7 KB

Contents

module GeoCalc
  module Bearing
    # @returns [Numeric] bearing in degrees
    def bearing_to point
      GeoCalc::Bearing.bearing_to self, point
    end

    # Returns the (initial) bearing from this point to the supplied point, in degrees
    # see(http:#williams.best.vwh.net/avform.htm#Crs)
    # 
    # @param [GeoPoint] Latitude/longitude of destination point
    # 
    # @return [Numeric] initial bearing in degrees from North
    #
    def self.bearing_to base_point, point
      lat1 = base_point.lat.to_rad
      lat2 = point.lat.to_rad
      dlon = (point.lon - base_point.lon).to_rad

      y = Math.sin(dlon) * Math.cos(lat2)
      x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(dlon)
      bearing = Math.atan2(y, x)

      (bearing.to_deg + 360) % 360
    end

    def final_bearing_to point
      GeoCalc::Bearing.final_bearing_to self, point
    end

    # Returns final bearing arriving at supplied destination point from this point; the final bearing 
    # will differ from the initial bearing by varying degrees according to distance and latitude
    # 
    # @param [GeoPoint] Latitude/longitude of destination point
    #
    # @return [Numeric] final bearing in degrees from North
    #
    def self.final_bearing_to base_point, point
      # get initial bearing from supplied point back to this point...
      lat1 = point.lat.to_rad
      lat2 = base_point.lat.to_rad
      dlon = (base_point.lon - point.lon).to_rad

      y = Math.sin(dlon) * Math.cos(lat2)
      x = Math.cos(lat1)*Math.sin(lat2) - Math.sin(lat1)*Math.cos(lat2)*Math.cos(dlon)
      bearing = Math.atan2(y, x)

      # ... & reverse it by adding 180°
      (bearing.to_deg+180) % 360
    end 
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
geo_calc-0.7.8 lib/geo_calc/calc/bearing.rb
geo_calc-0.7.7.1 lib/geo_calc/calc/bearing.rb
geo_calc-0.7.7 lib/geo_calc/calc/bearing.rb
geo_calc-0.7.6 lib/geo_calc/calc/bearing.rb
geo_calc-0.7.5 lib/geo_calc/calc/bearing.rb
geo_calc-0.7.4 lib/geo_calc/calc/bearing.rb