[英]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.
x1,y1在t秒,x2,y2在t + 1秒。 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.
另外,我想摆脱使用昂贵的atan2的麻烦。 Is there another formula I can use for this?
我可以为此使用另一个公式吗? Thanks!
谢谢!
Edit1: 编辑1:
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. Edit2:这就是我转弯的意思:我正在处理来自并非100%真实的交通模拟数据。 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.
我需要将汽车的行进方向倾斜20度左右。 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 假设您有一辆汽车,在时间t和时间t + 1 x2,y2处以点x1,y1的速度u,v行驶,则方向为
int turnleft = sgn(-v * (x2-x1) + u * (y2-y1));
With sgn
according to Is there a standard sign function (signum, sgn) in C/C++? 用
sgn
根据是否有在C / C ++标准符号函数(正负号,SGN)? 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.
因此,如果您左转,左转为1;如果笔直,则为0;如果您右转,则为-1。
Ah I forgot: There is also the case where you don't turn at all. 啊,我忘记了:在某些情况下,您根本不转弯。 Updated accordingly
据此更新
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.