给定圆上两点的度数,它们之间的度数是多少?

所以,我正在尝试找出圆上两点之间的角度。问题在于在诸如340和20之间的情况下,正确的答案是40度,但是做类似这样的事情

function FindLeastDegrees(s, f)
    return ((f - s+ 360) % 360)
end

print(FindLeastDegrees(60, 260))

-- S = Start, F = Finish (In degrees)

这在所有情况下都有效,除了尝试弄清两者之间的距离。这下面的代码是我接下来的失败尝试。

function FindLeastDegrees(s, f)
    local x = 0
    if math.abs(s-f) <= 180 then
        x = math.abs(s-f)
    else
        x = math.abs(f-s)
    end
return x
end

print(FindLeastDegrees(60, 260))

然后我试过:

function FindLeastDegrees(s, f)
    s = ((s % 360) >= 0) and (s % 360) or 360 - (s % 360);
    f = ((f % 360) >= 0) and (f % 360) or 360 - (f % 360);
    return math.abs(s - f)
end

print(FindLeastDegrees(60, 350))

--> 290 (Should be 70)

所以失败了。

那么,如何找到两个不同角度之间的最短角度,然后如果您需要顺时针或逆时针(加或减)到达那里。我完全困惑了。

我正在尝试做的一些示例...

FindLeastDegrees(60, 350)
--> 70

FindLeastDegrees(-360, 10)
--> 10

这似乎是这么难!我知道我必须使用...

  1. 取模运算
  2. 绝对值?

我还希望它返回我是否应该添加或减去到达值'Finish'。

对于冗长的描述感到抱歉,我想你可能已经懂了.... :/

原文链接 https://stackoverflow.com/questions/10238094

点赞
stackoverflow用户344949
stackoverflow用户344949

如果角度在0到360度范围内,则可以省略%360部分:

function FindLeastDegrees(s, f)
    diff = math.abs(f - s) % 360;
    return math.min(360 - diff, diff)
end
2012-04-19 23:11:47