Created
October 4, 2012 13:12
-
-
Save emiltin/3833444 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #!/usr/bin/env ruby | |
| require 'bigdecimal' | |
| DEG_TO_RAD = 0.017453292519943295769236907684886 | |
| EARTH_RADIUS_IN_METERS = 6372797.560856 | |
| def approximateDistance lat1, lon1, lat2, lon2 | |
| #assert(lat1 != INT_MIN) | |
| #assert(lon1 != INT_MIN) | |
| #assert(lat2 != INT_MIN) | |
| #assert(lon2 != INT_MIN) | |
| #Earth's quatratic mean radius for WGS-84 | |
| latitudeArc = (DEG_TO_RAD*(lat1-lat2))/100000.0 | |
| longitudeArc = (DEG_TO_RAD*(lon1-lon2))/100000.0 | |
| latitudeH = Math.sin( latitudeArc * 0.5 ) | |
| latitudeH *= latitudeH | |
| lontitudeH = Math.sin( longitudeArc * 0.5 ) | |
| lontitudeH *= lontitudeH | |
| tmp = Math.cos( (DEG_TO_RAD*lat1)/100000.0 ) * Math.cos( (DEG_TO_RAD*lat2)/100000.0 ) | |
| distanceArc = 2.0 * Math.asin( Math.sqrt( latitudeH + tmp * lontitudeH ) ) | |
| EARTH_RADIUS_IN_METERS * distanceArc | |
| end | |
| def simplified lat1, lon1, lat2, lon2 | |
| EARTH_RADIUS_IN_METERS * DEG_TO_RAD * Math.sqrt( (lat2-lat1)*(lat2-lat1) + (lon2-lon1)*(lon2-lon1) ) / 100000.0; | |
| end | |
| #calculate constant for use in distance tests | |
| def ap lat1, lon1, lat2, lon2 | |
| BigDecimal.new("1")/(BigDecimal.new("6372797.560856")*BigDecimal.new("0.017453292519943295769236907684886")); | |
| end | |
| lat1 = 1*100000.0 | |
| lon1 = 1*100000.0 | |
| lat2 = 1*100000.0 | |
| lon2 = 1.0008990679362704*100000.0 | |
| puts approximateDistance(lat1,lon1,lat2,lon2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment