获得两个标题之间的区别

时间:2011-02-17 02:11:48

标签: c# compass-geolocation 360-degrees

我有这种方法来确定2 0-360指南针标题之间的区别。

虽然这可以用来确定我的绝对距离(例如,总是正输出),但我无法弄清楚要将标志引入输出需要做些什么。

理想情况下,如果从初始航向到最终航向的最短距离是顺时针方向,我希望error有一个正号,如果标题之间的最短距离涉及到逆时针方向走动我希望error有一个负号。

所需输入/输出的一些例子

initial - final - error

0 .................... 30 .......... 30

30 .................... 0 .......... -30

360 .................... 1 .......... 1

1 .................... 360 .......... -1

代码:

    /// <summary>
    /// Calculate the error from a given initial heading to a final heading
    /// </summary>
    /// <param name="inital"></param>
    /// <param name="final"></param>
    /// <returns></returns>
    private double GetHeadingError(double initial, double final)
    {
        double directionA = final - initial;
        double directionB = 360 - (final + initial);
        double error = 0;

        if (Math.Abs(directionA) < Math.Abs(directionB))
        {
            error = directionA;
        }
        else
        {
            error = directionB;
        }

        return error;
    }

5 个答案:

答案 0 :(得分:15)

修改:添加了检查差异恰好是180度的时间。以前这是返回180或-180,取决于最终是否大于或低于初始。我已修改它,以便在两种情况下都返回正数。


所以这是我的尝试......

private static double GetHeadingError(double initial, double final)
        {
            if (initial > 360 || initial < 0 || final > 360 || final < 0)
            {
                //throw some error
            }

            var diff = final - initial;
            var absDiff = Math.Abs(diff);

            if (absDiff <= 180)
            {
                //Edit 1:27pm
                return absDiff == 180 ? absDiff : diff;
            }

            else if (final > initial)
            {
                return absDiff - 360;
            }

            else
            {
                return 360 - absDiff;
            }
        }

答案 1 :(得分:2)

如果我理解正确,我认为以下代码应该有效:

private double GetHeadingError(double initial, double final)
{
            if(initial == 360) initial = 0;
            if(final == 360) final = 0;
            double clockWise = (final - initial);
            double counterClockWise = (360 - final + initial);
            return (Math.Abs(clockWise) <= Math.Abs(counterClockWise)) ? clockWise : -counterClockWise;
}

基本上我将360度视为0,我相信这是可以的。此代码将生成与上表中列出的结果相同的结果。代码不进行边界检查,期望值介于0到360之间。

答案 2 :(得分:1)

我认为您所需结果的表格不正确。这是我笨拙的方式:

private double MyGetHeadingError(double initial, double final)
{
    initial += 1000;
    final += 1000;

    bool flipped = false;
    if (initial > final)
    {
        double temp;
        temp = final;
        final = initial;
        initial = temp;
        flipped = true;
    }
    double error;
    if (final - initial > 180)
        final = final - 360;

    error = final - initial;

    if (flipped == true)
        error = -error;
    return error;
}

答案 3 :(得分:1)

Degree_Diff = (MIN(ABS(ENDCOMPASS-STARTCOMPASS),ABS(360-ENDCOMPASS+STARTCOMPASS),ABS(360-STARTCOMPASS+ENDCOMPASS))) 

答案 4 :(得分:0)

这是一个简单的解决方案,尽管在Dart中命名有所不同。基于this avionics answer

/// The difference of two headings in degrees such that it is always in the range
/// (-180, 180]. A negative number indicates [h2] is to the left of [h1].
double headingDiff(double h1, double h2) {
  double left = h1 - h2;
  double right = h2 - h1;
  if (left < 0) left += 360;
  if (right < 0) right += 360;
  return left < right ? -left : right;
}

编辑:还有一个更简洁的答案here,但我自己还没有尝试过:

double headingDiff(double h1, double h2) => (h2 - h1 + 540) % 360 - 180;