0

I have a dataset with a list of points. The points represents something that is moving on a territory, so I have for each point the current position (x, y) and the future position (target_x, target_y), and the distance between current and future position.

How can I calculate in R for each point the angle and direction between their current and future position? For example if that point is moving to Northeast the value I would like to obtain would be close to 45º, while it would be 180º if is moving to Southwest.

Thanks

Phil
  • 7,287
  • 3
  • 36
  • 66
BAdm
  • 1
  • 1
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Mar 25 '21 at 18:23
  • Welcome to stack overflow! What have you tried so far? What would you do if you only had one point to worry about? – rcorty Mar 25 '21 at 18:43
  • The table would be something similar to this: POINT_X POINT_Y target_x target_y distance 585578 4118669 617487 4149767 44369 585578 4118669 550824 4143119 42044 These are UTM coordinates. In the first point, If I go from the original coordinates to the target coordinates, the angle/direction to reach it should be close to 45º. On the contrary, in the second point, should be close to 315º as it is in the opposite direction. If the Y is different but the X doesnt vary, the values I need to calculate should be 0º or 180º depending on the direction. – BAdm Mar 25 '21 at 18:59
  • 2
    If you have two points `(x1, y1)` and `(x2, y2)` then the angle is `a = arctan( (y2 - y1) / (x2 - 1) )`. If you need the angle in radians not in degrees you should multiply `a` by `180 / Pi`. – Yuri Ginsburg Mar 25 '21 at 23:16
  • An `x` was forgotten in the previous comment, plus the R function for the arc-tangent seems to be `atan`, thus: `a = atan( (y2 - y1) / (x2 - x1) )` – matmar Jan 19 '23 at 11:39

0 Answers0