Module: GeoCalc::Midpoint

Defined in:
lib/geo_calc/calc/midpoint.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.midpoint_to(base_point, point) ⇒ Array

Returns the midpoint between this point and the supplied point. see(#mathforum.org/library/drmath/view/51822.html for derivation)

Parameters:

  • base_point: (GeoPoint)

    Starting point (latitude, longitude)

  • point: (GeoPoint)

    Destination point (latitude, longitude)

Returns:

  • (Array)

    Midpoint between this point and the supplied point



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/geo_calc/calc/midpoint.rb', line 14

def self.midpoint_to base_point, point
  lat1 = base_point.lat.to_rad
  lon1 = base_point.lon.to_rad;
  lat2 = point.lat.to_rad
  dlon = (point.lon - base_point.lon).to_rad

  bx = Math.cos(lat2) * Math.cos(dlon)
  by = Math.cos(lat2) * Math.sin(dlon)

  lat3 = Math.atan2(Math.sin(lat1)+Math.sin(lat2), Math.sqrt( (Math.cos(lat1)+bx)*(Math.cos(lat1)+bx) + by*by) )

  lon3 = lon1 + Math.atan2(by, Math.cos(lat1) + bx)

  [lat3.to_deg, lon3.to_deg]
  # GeoPoint.new 
end

Instance Method Details

#midpoint_to(point) ⇒ Object



3
4
5
# File 'lib/geo_calc/calc/midpoint.rb', line 3

def midpoint_to point
  GeoCalc::Midpoint.midpoint_to self, point
end