Describe the relative angles between the points (for example, the direction of movement)

I have a list of points with x, y coordinates. I know how to get the distance between points with sqrt(pow($x2 - $x1, 2) + pow($y2 - $y1, 2)) and the angle between points with atan2(y1 - y2, x1 - x2) .

How to calculate the relative angle between points (left, right, straight)? So, if I'm at point 1, what is the relative direction to point 2, then from 2 to 3, from 3 to 4, etc.

Thanks for any help!

+4
source share
3 answers

Call atan2($y1 - $y2, $x1 - $x2)

If it is between π / 2 and 3π / 2, it is straight.
If it is more than 3π / 2 or less than -3π / 2, this is a left turn.
If it is between -π / 2 and π / 2, this is a right turn.

Here is the chart:

  {3π / 2} ------- {π / 2} ------- {π / 4}
       |  + ----- [+ y] ----- + | 
       |  |  |  | 
       |  |  |  | 
     {π} [-x] [0,0] [+ x] {0} 
       |  |  |  | 
       |  |  |  | 
       |  + ----- [-y] ----- + |
 {-3π / 2} ------- {- π / 2} ------ {- π / 4} 
+2
source

If you know the angle from point 2 to 1 and point 2 to 3, this is just a question of angle_to_3 - angle_to_1 (or angle to the next minus the angle to the previous one), which you know how to calculate using atan2, or am Am I mistaken? Left / right / right rounds the result. If you want to do this in a complicated way, you can rotate your points so that the angle from 2 to 1 is 0, but that seems overly confusing.

0
source

Given your example of driving directions, the concept of left / right / straight only makes sense with respect to the current direction of travel. This is why SatNav devices start on the first move or “Please go to the planned route” when you disconnect it from the map and not immediately turn left / right / etc.

If you know the angle x1, y1-x2, y2, then you can give an indication of the relative direction for the next point using the angle x2, y2-x3, y3. If the next angle is larger, then it is likely to be a left turn, less - a turn to the right and within a certain field around the first corner at which it will be straight.

0
source

Source: https://habr.com/ru/post/1312983/


All Articles