Last active
July 27, 2017 19:15
-
-
Save moniyax/cf72638350718544c6b125c36f89554b to your computer and use it in GitHub Desktop.
Coordinates of a point on surface of earth at a distance and bearing from given point
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
# http://www.movable-type.co.uk/scripts/latlong.html | |
# Destination point given distance and bearing from start point | |
# Radius of earth | |
R=6371 | |
# Return the lat2 & lon2 of a point at distance d and | |
# bearing brng from an initial point wt coords lat1 & lon1 | |
def coords_at_dist lat1, lon1, d, brng | |
# lat1 => latitude at initial point | |
# lon1 => longitude at initial point | |
# brng => bearing towards final point | |
# d => distance to final point | |
lat1 = deg2rad lat1 | |
lon1 = deg2rad lon1 | |
brng = deg2rad brng | |
lat2 = Math.asin(Math.sin(lat1)*Math.cos(d/R)+Math.cos(lat1)*Math.sin(d/R)*Math.cos(brng)) | |
lon2 = lon1 + Math.atan2(Math.sin(brng)*Math.sin(d/R)*Math.cos(lat1), Math.cos(d/R)-Math.sin(lat1)*Math.sin(lat2)) | |
# formula for normalizing longitude | |
# lon2 = (lon2+540)%360-180 | |
[brk(lat2), brk(lon2)] | |
end | |
# Turns angle in radians to [degrees, minutes] | |
def brk rad | |
deg = rad2deg rad | |
[deg.to_i, ((deg-deg.to_i)*60).abs] | |
end | |
def deg2rad deg | |
deg * Math::PI / 180 | |
end | |
def rad2deg rad | |
rad * 180 / Math::PI | |
end | |
# res = coords 6.5, 3.4, 120.0, 32.0 | |
# res = coords 0, 20.4, 1800.0, 95.0 | |
res = coords_at_dist 5.6037, 0, 1000.0, -30.0 | |
print res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment