简体   繁体   English

24小时范围交叉逻辑

[英]24 hour ranges intersection logic

I have schedules which can go from 21:00 to 04:00. 我的时间表可以从21:00到04:00。
Mostly these schedules will be such that they might pass the 00:00 hour mark. 通常,这些时间表可能会超过00:00小时。
Now i also have to add normal schedules spanning from 00:00 to 23:59. 现在我还必须添加从00:00到23:59的正常时间表。
I would like to calculate an intersection for the same. 我想为它计算一个交集。
currently i am using 目前我正在使用

if(((mytime1.start_time <= mytime2.end_time) && (mytime2.start_time <= mytime1.end_time))).

I still don't have a valid fool proof logic for the intersection when the trans day schedules are taken into account please help. 当考虑到跨日行程时,我仍然没有有效的防错逻辑,请帮忙。

bool DoIntervalsOverlap(int s0, int e0, int s1, int e1)
{
    return s0 - e0 <= (unsigned int) s0 - e1 || s1 - e1 <= (unsigned int) s1 - e0;
}

First, note that, in essence, all of the arithmetic is unsigned. 首先,请注意,实质上,所有算术都是无符号的。 In s0 - e0 <= (unsigned int) s0 - e1 , e1 is converted to unsigned int to match s0 , and s0 - e0 is converted to unsigned int to match (unsigned int) s0 - e1 . s0 - e0 <= (unsigned int) s0 - e1 ,将e1转换为unsigned int以匹配s0 ,将s0 - e0转换为unsigned int以匹配(unsigned int) s0 - e1 Below, I assume all arithmetic is unsigned. 下面,我假设所有算术都是无符号的。

In retrospect, I wish I had written the terms in reverse order. 回想起来,我希望我以相反的顺序写这些术语。 Let's fix that now. 让我们现在修复它。 s0 - e0 <= s0 - e1 is equivalent to e0 - s0 >= e1 - s0 . s0 - e0 <= s0 - e1等效于e0 - s0 >= e1 - s0 (This is true even in unsigned arithmetic.) Now we can think of e0 - s0 and e1 - s0 as the times e0 and e1 translated to a reference frame in which s0 is at the origin. (即使在无符号算术中也是如此。)现在我们可以将e0 - s0e1 - s0视为将e0e1转换为以s0为原点的参考帧的时间。 In this frame, any times that are earlier in the day than the original s0 have been wrapped to large positive numbers. 在此框架中,每天中比原始s0早的任何时间都被包装为大正数。 So, the wrapping around midnight is gone. 因此,午夜时分的包裹不见了。 We have only non-negative times measured from s0 . s0开始只有非负时间。 Then we see that e0 - s0 >= e1 - s0 is asking “Is e1 , measured from s0 , less than or equal to e0 ?” That question is equivalent to ”Is e1 inside [ s0 , e0 ]?” 然后我们看到e0 - s0 >= e1 - s0在问“从s0测得的e1是否小于或等于e0 ?”的问题等同于“在[ s0e0 ]内的e1是吗?”

Thus, the two conditions ask “Is e1 inside [ s0 , e0 ] or is e0 inside [ s1 , e1 ]?” If either interval ends inside the other, the intervals overlap. 因此,这两个条件询问“[S0,E0]E1或为[S1,E1]的e0?”如果任一间隔在另一个之内结束,则间隔重叠。 If neither ends inside the other, they do not overlap. 如果两端都在另一端,则它们不会重叠。

Consider each instant of time in the first interval in order. 按顺序考虑第一个时间间隔中的每个时刻。 If none of these instants coincides with the start of the second interval then the two intervals do not intersect, unless the intervals were already intersecting from the start, in which case the first instant in the first interval will occur in a similar pass along the second interval. 如果这些时刻都与第二个间隔的开始不重合,则这两个间隔不会相交,除非这些间隔已经与开始相交,在这种情况下,第一个间隔中的第一个瞬时将沿着第二个间隔以类似的方式发生间隔。 So if the starts and ends are (l1, r1) for the first interval and (l2, r2) for the second, we can check by looking to see if l1 is included in the range [l2, r2] and if l2 is included in the range [l1, r1]. 因此,如果第一个间隔的开始和结束是(l1,r1),第二个间隔的开始和结束是(l2,r2),我们可以通过查看是否在范围[l2,r2]中包括l1以及是否包括l2来进行检查在[l1,r1]范围内。 If lx is numerically no larger than rx then this is the simple check to see if eg (l1 >= l2) && (l1 <= r2). 如果lx在数值上不大于rx,则这是简单的检查,看是否(l1> = l2)&&(l1 <= r2)。 If lx is larger than rx then it is a wraparound interval and you could check if (l2 >= l1) || 如果lx大于rx,则它是一个环绕间隔,您可以检查(l2> = l1)|| (l2 <= r1). (l2 <= r1)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM