测试数字是否在循环间隔内 [英] Test if number is inside circular interval
问题描述
让我们假设我们有一个数字圈,范围从-180到180,看起来像这样:
Let us suppose we have a number circle, that ranges from -180 to 180, looking something like this:
180/-180
***
*** ***
90 *** *** -90
*** ***
***
0
圆圈的一部分始终沿顺时针方向扫过.您怎么知道数字是在扫描间隔内还是在扫描间隔外?
A section of the circle is always swept in a clockwise direction. How can you tell if a number is inside or outside of the interval swept?
在下面的示例I/O中,前两个数字表示间隔,第三个数字是要检查的数字.如果该点(包括端点)在区间内,则输出为true,否则为false.
In the following sample I/O, the first two numbers represent the interval and the third number is the number being checked. Output is true if the point is (inclusively) inside the interval, false otherwise.
2 4 6
False
2 4 4
True
90 -90 0
False
90 -90 -180
True
推荐答案
-
将您的数字从0标准化到359.考虑参数a,b和c(c在a-> b的扫描范围内).正如克里斯·坎宁安(Chris Cunningham)所指出的,您还可以将其标准化为-180至+179;请参阅下面的讨论.归一化的重要部分是确保仅一个数字指向圆上的每个点.
如果
(a< = b)
,则返回(c> = a&& c< = b)
否则,您已经扫过了0点,应该返回
(c> = b || c< = a)
(c> = a || c< = b)
else you've swept across the 0 point and should return
(c >= b || c <= a)
(c >= a || c <= b)
这篇关于测试数字是否在循环间隔内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!