我知道,这是一个愚蠢的问题,但我需要检查两个时间段(以纪元
为单位)是否相互重叠。我只是不知道这些检查是否足够。
TLPeriod.prototype.isOverlapping = function(period) {
if( period.toPoints().start.getEpoch() < this.toPoints().start.getEpoch()
&&
this.toPoints().start.getEpoch() < period.toPoints().end.getEpoch())
return true;
if(this.toPoints().end.getEpoch() > period.toPoints().start.getEpoch())
return true;
return false;
};
我知道,我应该写here ,但要花很多时间才能得到答案。
可以快速总结为:
同轴上的两条线带有点:
|(this.start),(this.end)|
&
|(期间.开始),(期间.结束)|
如何检查它们是否重叠?
OVERLAP!
|-----------|-----------------|-------------|
this.start period.start this.end period.end
NO OVERLAP!
|-----------| |-------------|
this.start this.end period.start period.end
OVERLAP!
|-----------------|--------|-------------|
period.start this.start this.end period.end
最佳答案
一个相反的问题:它们什么时候不重叠?答案:当第一个在第二个结束后开始时,或者当第二个在第一个结束后开始时。所以
TLPeriod.prototype.isOverlapping = function(period) {
return !(
period.toPoints().start.getEpoch() > this.toPoints().end.getEpoch() ||
this.toPoints().start.getEpoch() > period.toPoints().end.getEpoch()
);
}
关于javascript - 如何检查两个 'lines'是否重叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55599986/