2012-04-19 52 views
1

注意:我正在使用Lua。给定一个圆圈上的两个点相对于他们的度数,他们之间的度数是多少?

所以,我试图找出圆上两点之间的度数。问题是像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. 绝对值?

我也想要它返回,如果我应该增加或减去获得值'完成'。
对不起,冗长的说明,我想你也许已经知道了....:/

+0

这是链接http://stackoverflow.com/questions/16460311/determine-angle-between-two-points-on-a-circle-with-respect-to-center/16460479#16460479 – 2013-05-09 11:40:29

回答

2

如果度都在0至360范围内,% 360部分可以跳过:

function FindLeastDegrees(s, f) 
    diff = math.abs(f-s) % 360 ; 
    return math.min(360-diff, diff) 
end 
+0

他们不是。然而,我是否可以这样做: function FindLeastDegrees(s,f) s =((s%360)> = 0)和(s%360)或360-(s%360); (f%360)> = 0)和(f%360)或360-(f%360); 返回math.min(360 math.abs(F-S),math.abs(F-S)) 端 打印(FindLeastDegrees(60,260)) 随着额外的代码作为代码来获取度? – Stormswept 2012-04-19 23:15:42

+0

不需要这种并发症。看我的编辑。 – 2012-04-19 23:22:50

+0

啊。我看到你在那里做了什么!谢谢! – Stormswept 2012-04-19 23:46:52

相关问题