An R function I had to write to calculate the true course/bearing between two lat lon positions. Note that the fossil R library has a function called earth.bear to do the same thing, but hey, who’s installing more libraries at this point?
#finds the bearing aka true course (tc) between two positions
getBearing<-function(lon1, lat1, lon2, lat2) {
#lon1<-deg2rad(lon1)
lat1<-deg2rad(lat1)
#lon2<-deg2rad(lon2)
lat2<-deg2rad(lat2)
lambda<-deg2rad(lon2-lon1)
Y<-sin(lambda)*cos(lat2)
X<-cos(lat1)*sin(lat2)-sin(lat1)*cos(lat2)*cos(lambda)
tc<-atan2(Y,X)
tc<-rad2deg(tc)
if (tc<0.0) {
tc<-360+tc
}
tc
}
getBearing(-94.581, 39.099, -90.200,38.627)
#Note that you have to convert degrees to rads and back to make this work, here are the helper functions to do that:
deg2rad <- function(deg) return(deg*pi/180) rad2deg <- function(rad) return((rad*180)/pi)