3

Using geosphere::bearing I can calculate the bearing of two lines, but is it possible to calculate the angle between the two bearings ?

Of course you can try and subtract or sum up the bearings but in specific cases where one is negative and the other is positive this doesn't work.

For example if the ber1 = - 175 and ber2 = 175 the angle between should be 10.

Any suggestions ?

adl
  • 1,390
  • 16
  • 36
  • Take the absolute difference (mod 360) between the angles and subtract it from 360 if it is greater than 180. – meowgoesthedog May 31 '18 at 11:36
  • `abs(ber1) - abs(ber2) = 0` -> subtracting 0 from 360 is equal to 360, the point I'm trying to make is that with implementing many conditions it might be possible to get the correct answer, but maybe there is a function that already has all these rules implemented – adl May 31 '18 at 11:37
  • 1
    *If it is greater than 180*. And should be `abs(ber1 - ber2)`. If you're merely looking for an existing function then Google is your friend. – meowgoesthedog May 31 '18 at 11:38
  • For a `C#` and `C` solution, see [Turn Direction for Target Heading](//stackoverflow.com/q/12869557). – Joseph Quinsey Jan 24 '19 at 15:39

1 Answers1

3

I am not sure of a ready-made package but in case you are interested in a solution then you can try

angle_diff <- function(theta1, theta2){
  theta <- abs(theta1 - theta2) %% 360 
  return(ifelse(theta > 180, 360 - theta, theta))
  }

which gives the angle between your example bearings -175 & 175 as

angle_diff(-175, 175)
#[1] 10
Prem
  • 11,775
  • 1
  • 19
  • 33