简体   繁体   中英

Formula to find the +/- sign of the angle between two points?

I've been trying to find a formula to figure out if a car is turning left or right given two sets of coordinates. x1,y1 is at t seconds, and x2,y2 is at t+1 seconds. Up until now, I've been using this:

double direction = atan2(y2 - y1, x2 - x1)

Then, I check to see if the direction is positive or negative to find out if the car is turning left or right. This works, but I don't need to know the value of the direction at all. I just need the sign. Plus, I'd like to get away from using atan2 which can be expensive. Is there another formula I can use for this? Thanks!


Edit1:

I have velocity. The time between the two points is always one second apart, so it can be calculated if needed.


Edit2: Here's what I mean by turning: I'm dealing with data from a traffic simulation that's not 100% realistic. Normally, I have the heading (in degrees) of the car. However, when a car changes lanes, the heading is inaccurate (appears to travel diagonally). So, in this case I've already determined that the car is changing lanes. I need to skew the car's heading by 20 degrees or so. I just need to figure out which direction to change the heading, which is why I need to figure out what direction the car is "turning" in while changing lanes.

当且仅当y分量为正时, atan2为正。

Assuming you have a car, driving with velocity u,v at point x1,y1 at time t and at time t+1 x2,y2 then the direction is given by

int turnleft = sgn(-v * (x2-x1) + u * (y2-y1));

With sgn according to Is there a standard sign function (signum, sgn) in C/C++? for example.

Some more explanation:

You turn left if the scalar product between the position change and the normal velocity is greater than zero. The rotation of the velocity is to the left (counter clockwise). So turnleft is 1 if you turn left, 0 if you go straight and -1 if you turn right.

Ah I forgot: There is also the case where you don't turn at all. Updated accordingly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM