Created
March 13, 2009 21:43
-
-
Save alandipert/78784 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DistanceCalculator | |
#radians per degree | |
RAD_PER_DEG = 0.017453293 | |
#radius of the earth, in miles | |
Rmiles = 3956 | |
def self.get_distance(lat1, lon1, lat2, lon2) | |
dlon = lon2 - lon1 | |
dlat = lat2 - lat1 | |
dlon_rad = dlon * RAD_PER_DEG | |
dlat_rad = dlat * RAD_PER_DEG | |
lat1_rad = lat1 * RAD_PER_DEG | |
lon1_rad = lon1 * RAD_PER_DEG | |
lat2_rad = lat2 * RAD_PER_DEG | |
lon2_rad = lon2 * RAD_PER_DEG | |
a = (Math.sin(dlat_rad/2))**2 + Math.cos(lat1_rad) * Math.cos(lat2_rad) * (Math.sin(dlon_rad/2))**2 | |
c = 2 * Math.atan2( Math.sqrt(a), Math.sqrt(1-a)) | |
(Rmiles * c).floor | |
end | |
end | |
#should be 89 miles from vista to LA | |
puts DistanceCalculator.get_distance(33,-117,34,-118) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment