测试数字是否在循环区间内
假设我们有一个范围从 -180 到 180 的数字圆,看起来像这样:
180/-180
***
*** ***
90 *** *** -90
*** ***
***
0
圆的一部分总是以顺时针方向扫过。如何判断一个数字是在扫描区间之内还是之外?
在以下示例 I/O 中,前两个数字表示间隔,第三个数字是正在检查的数字。如果该点(包含)在区间内,则输出为 true,否则为 false。
2 4 6
False
2 4 4
True
90 -90 0
False
90 -90 -180
True
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?
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将数字标准化为 0 到 359。考虑参数 a、b 和 c(c 位于 a -> b 的扫描范围内)。 正如 Chris Cunningham 所指出的,您还可以标准化为 -180 到 +179;请参阅下面的讨论。标准化的重要部分是确保只有一个数字指代圆上的每个点。
如果
(a <= b)
则返回(c > ;= a && c <=
否则你已经扫过了 0 点,应该返回
(c >= b || c <= a)
(c >; = a || c <= b)
Normalize your numbers from 0 to 359. Consider the arguments a, b and c (is c inside the sweep of a -> b). As pointed out by Chris Cunningham, you can also normalize to -180 to +179; see discussion below. The important part of normalization is to make sure only one number refers to each point on the circle.
If
(a <= b)
then return(c >= a && c <= b)
else you've swept across the 0 point and should return
(c >= b || c <= a)
(c >= a || c <= b)
[a,b] 中的所有点 x 验证:
如果 a%360<=b%360:
(x)%360<=b%360 且 x%360>=a%360
如果你直接处理。否则你的intervall包含0,你可以验证一下。 x in[a,b]
因此:
执行您需要的操作。
我可能会颠倒一些事情..所以你可能需要再次检查。
All the points x that are in [a,b] verifies :
if a%360<=b%360:
(x)%360<=b%360 and x%360>=a%360
if you process in the direct sens.otherwise your intervall contains 0, and you can just verify. x in[a,b]
therefore:
does what you need.
I might inverse some things.. so you wil may be need to check it again.
您将
start
和end
作为间隔的端点。You have
start
andend
as endpoints of the intervall.