Rutils/maybe-not-useful/xypath.r

#==========================================================================================#
#==========================================================================================#
#      This function transforms the coordinates of a point to coordinates relative to the  #
# path.                                                                                    #
#------------------------------------------------------------------------------------------#
xypath <<- function(x,y,xpath,ypath,dxtrans,tangle){

   #----- Ensure tangle is between 0 and 360. ---------------------------------------------#
   tangle = tangle %% 360
   #---------------------------------------------------------------------------------------#

   #---------------------------------------------------------------------------------------#
   #      Find the distance between adjacent points along path.                            #
   #---------------------------------------------------------------------------------------#
   along = sqrt( ( xpath[-1] - xpath[-npath] )^2 + ( ypath[-1] - ypath[-npath] )^2 )
   along = cumsum(c(0,along))
   along = along + 0.5 * dxtrans - mean(along)
   #---------------------------------------------------------------------------------------#

   #----- Find the closest point. ---------------------------------------------------------#
   dist  = sqrt( ( x - xpath )^2 + ( y - ypath) ^ 2 )
   idx   = which.min(dist)
   #---------------------------------------------------------------------------------------#

   #------ The "x" coordinate is always along the path. -----------------------------------#
   xout = along[idx]
   #---------------------------------------------------------------------------------------#


   #---------------------------------------------------------------------------------------#
   #       The "y" coordinate depends upon the transect angle.                             #
   #---------------------------------------------------------------------------------------#
   if (tangle < 45.){
      yout = ifelse(y > ypath[idx],1,-1) * dist[idx]
   }else{
      yout = ifelse(x < xpath[idx],1,-1) * dist[idx]
   }#end if (tangle < 45.)
   #---------------------------------------------------------------------------------------#



   #----- Final answer. -------------------------------------------------------------------#
   ans = c(x = xout, y = yout)
   return(ans)
   #---------------------------------------------------------------------------------------#
}#end xypath
#==========================================================================================#
#==========================================================================================#
manfredo89/ED2io documentation built on May 21, 2019, 11:24 a.m.