Suppose we have two half-open intervals
[A, B) [X, Y)
and we want to know whether they intersect. In any particular case, it's obvious. For example
[1, 2) [3, 4) disjoint
[1, 5) [3, 4) intersect
But can we write a rule that covers the general case? Let's see...the first interval could contain the second, or it could overlap on the left, or on the right, or...this is getting complicated...
Here's how to do it.
There are 24 permutations of the 4 endpoints, but only 6 satisfy the conditions A < B and X < Y. These are
A B X Y
A X B Y intersect
A X Y B intersect
X A B Y intersect
X A Y B intersect
X Y A B
The four in the middle intersect, and the two on the ends do not.
The first is identified by B <= X, a condition that holds for it and no other, and the last is identified by Y <= A, a condition that holds for it and no other. Both comparison have an open bound on the left and a closed bound on the right, so a <= comparison is appropriate.
We can identify the four intersecting cases with the condition
NOT (B <= X OR Y <= A)
or, distributing the NOT
X < B AND A < Y
本文探讨了两个半开区间是否相交的问题,通过分析四种可能的交集情况,提出了一个简洁的数学条件来判断任意两个半开区间的交集状态。
649

被折叠的 条评论
为什么被折叠?



