Given multiple coordinates (x,y) of a vehicle, how to determine the angle at which the vehicle turns?

492 Views Asked by At

Around 20 coordinates of a vehicle's location are mentioned. Everytime the car turns, at what angle does it turn? How to determine the angle? Is there any particular formula to get the angle using coordinates only?

I tried coding it in Python:

def turn_angle(x2, y2, x1, y1, rotation=0, clockwise=False):
    angle = degrees(atan2(y2-y1, x2-x1)) - rotation
    if not clockwise:
        angle = -angle
    return angle % 360

Is the above function correct? Or is there any other alternative method to calculate?

A few coordinates of the vehicle are:

(0,0), (-0.2,-0.5), (-0.2,-0.5), (-0.1,-0.6), (0.3,-0.2),(1.2,0), 

(2.6,-0.5), (4.3,-1.2), (5.5,-2.7), (6.5,-4.8), (6.7,-7.8), (5.7,-11.2), 

(3.9,-14.8), (2.2,-17.8), (0.5,-20.8), (-2.1,-24.7), (-4.9,-30), (-8.5,-37.1), 

(-11.9,-44.7), (-14.7,-52.8), (-17.7,-61.8), (-19.9,-70.9), (-21.5,-80), (-23.3,-89.4),

(-24.9,-97.9), (-26.6,-105.6), (-27.8,-112.3), (-28.6,-117.7), (-28.8,-120.1)
2

There are 2 best solutions below

4
On

The problem doesn't seem well-defined. How many turns and by what angle can you see in these four points?

enter image description here

2
On

I agree with the comments, this problem is not well defined.

A car moves in consecutive arcs and lines. The points provided are not sufficient to define the path of the car.

For example, take three points below:

fig1

You can either fit one arc through them - case a), or two arcs through them - case b).

The end result has the vehicle pointing in different directions past the 3rd point.

With the points given, the path looks as follows:

fig2

The sense of direction can be found by the sign of the distance $h$ of the 3rd point from the line connecting the 1st and 2nd points

fig3

m = sqrt((x_1-x_2)^2+(y_1-y_2)^2)
h = (x_1*(y_2-y_3)+x_2*(y_3-y_1)+x_3*(y_1-y_2))/m
turn = sign(h)