Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 方向确定算法混合了左右方向_C#_Algorithm - Fatal编程技术网

C# 方向确定算法混合了左右方向

C# 方向确定算法混合了左右方向,c#,algorithm,C#,Algorithm,我正在研究一种算法,它可以确定边BC相对于边AB的方向 我已经实施了ArcCos determination。它几乎起作用了。它正确地确定前进方向,并正确地确定何时需要转弯。但无法正确确定是右转还是左转 private Direction DetermineDirectionOfV2(Vertex v1, Vertex v2, Vertex v3) { if (v1 == null || v2 == null || v3 == null) return Direction

我正在研究一种算法,它可以确定边BC相对于边AB的方向

我已经实施了ArcCos determination。它几乎起作用了。它正确地确定前进方向,并正确地确定何时需要转弯。但无法正确确定是右转还是左转

private Direction DetermineDirectionOfV2(Vertex v1, Vertex v2, Vertex v3)
{
    if (v1 == null || v2 == null || v3 == null)
        return Direction.Forward;

    double p12 = v1.distance2D(v2);
    double p13 = v1.distance2D(v3);
    double p23 = v2.distance2D(v3);

    double p12S = p12 * p12;
    double p13S = p13 * p13;
    double p23S = p23 * p23;

    double a = p12S + p13S - p23S;
    double b = 2 * p12 * p13;

    if(b == 0.0)
         return Direction.Forward;

    double angle = Math.Acos(a / b);

    double thresh = 0.1;
    if(angle >= -thresh && angle <= thresh)
    {
        return Direction.Forward;
    }
    else if (angle > 0 && angle < Math.PI)
    {
        return Direction.Right;
    }
    else
    {
        return Direction.Left;
    }

}
给出一个0.46弧度的角度,实际上它应该是4.71238898弧度,因为从AB的角度来看,BC形成一个270度的角度

谢谢

范例

如果我们有如下几点:

A


B       C

这将产生270度或4.7弧度,因为如果我们从A走到B,我们将左转到C。

首先,你的分子符号错了。余弦法则是

c² = a² + b² - 2*a*b*cos(alpha)
因此,

cos(alpha) = (c² - a² - b²) / (2 * a * b)
在你的命名法中,这意味着:

angle = Math.Acos(p13S - p12S - p23S / (2 * p12 * p13));
不幸的是,这并不能解决你的问题

余弦法则给出了两个导管之间的最小角度。您只处理长度,因此会丢失方向信息。
acos
函数返回0到π范围内的角度,但需要从-π到π的整个圆

你的问题没有说清楚,但我想你在x,y平面上有二维向量。(如果没有参照平面,像向左和向右这样的方向就没有意义。)

如果是这样,你可以通过计算AB和BC的叉积来决定你是向左拐还是向右拐。然后检查z组件的符号:

private Direction dir(Vertex v1, Vertex v2, Vertex v3)
{
    double ax = v2.x - v1.x;
    double ay = v2.y - v1.y;
    double bx = v3.x - v2.x;
    double by = v3.y - v2.y;

    double z = ax*by - ay*bx;

    if (z > 0.0) return Direction.Left;
    if (z < 0.0) return Direction.Right;
    return Direction.Forward;
}
此外,前进方向也适用于后退方向。如果要区分这些方向,可以检查标量积

ax*bx + ay*by

是正(向前)还是负(向后)。

在这种情况下,我不确定如何使用Atan,您能给我举个例子吗?您需要使用atan2,而不是atan@Seb我不确定在这种情况下Y和X参数是什么。是a和b变量吗?
|a x b| = |a| * |b| * sin(angle)
ax*bx + ay*by