Sha256: f07bf8b9179b837eea928b4647e43ed44fe2212652d9d8e2cd74be66bd03735f

Contents?: true

Size: 1.11 KB

Versions: 2

Compression:

Stored size: 1.11 KB

Contents

module Mongoid
  module Spacial
    module Formulas
      RAD_PER_DEG = Math::PI/180

      def self.n_vector(point1,point2)
        p1 = point1.map{|deg| deg * RAD_PER_DEG}
        p2 = point2.map{|deg| deg * RAD_PER_DEG}

        sin_x1 = Math.sin(p1[0])
        cos_x1 = Math.cos(p1[0])

        sin_y1 = Math.sin(p1[1])
        cos_y1 = Math.cos(p1[1])

        sin_x2 = Math.sin(p2[0])
        cos_x2 = Math.cos(p2[0])

        sin_y2 = Math.sin(p2[1])
        cos_y2 = Math.cos(p2[1])

        cross_prod =  (cos_y1*cos_x1 * cos_y2*cos_x2) +
          (cos_y1*sin_x1 * cos_y2*sin_x2) +
          (sin_y1        * sin_y2)

        return cross_prod > 0 ? 0 : Math::PI if (cross_prod >= 1 || cross_prod <= -1) 

        Math.acos(cross_prod)
      end

      def self.haversine(point1,point2)
        p1 = point1.map{|deg| deg * RAD_PER_DEG}
        p2 = point2.map{|deg| deg * RAD_PER_DEG}

        dlon = p2[0] - p1[0]
        dlat = p2[1] - p1[1]

        a = (Math.sin(dlat/2))**2 + Math.cos(p1[1]) * Math.cos(p2[1]) * (Math.sin(dlon/2))**2

        2 * Math.atan2( Math.sqrt(a), Math.sqrt(1-a))
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mongoid_spacial-0.1.1 lib/mongoid_spacial/spacial/formulas.rb
mongoid_spacial-0.1.0 lib/mongoid_spacial/spacial/formulas.rb