我有一个三个经纬度坐标组成两条线段 A 到 B 到 C。我还找到了一个函数,它可以以 -180 到 180 的方式返回线段 A-B 或 B-C 的北方位。但是,我无法确定汽车何时从 A 到达 B,它应该向右转还是向左转以继续前往 C。
最佳答案
编辑:之前的答案是错误的。现在这是正确的
public Direction GetDirection(Point a, Point b, Point c)
{
double theta1 = GetAngle(a, b);
double theta2 = GetAngle(b, c);
double delta = NormalizeAngle(theta2 - theta1);
if ( delta == 0 )
return Direction.Straight;
else if ( delta == Math.PI )
return Direction.Backwards;
else if ( delta < Math.PI )
return Direction.Left;
else return Direction.Right;
}
private Double GetAngle(Point p1, Point p2)
{
Double angleFromXAxis = Math.Atan ((p2.Y - p1.Y ) / (p2.X - p1.X ) ); // where y = m * x + K
return p2.X - p1.X < 0 ? m + Math.PI : m ); // The will go to the correct Quadrant
}
private Double NormalizeAngle(Double angle)
{
return angle < 0 ? angle + 2 * Math.PI : angle; //This will make sure angle is [0..2PI]
}
关于c# - 如何计算转弯方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3419341/