如何检查两个 'lines' 是否重叠?

How to check if two 'lines' overlap?

我知道,这是一个愚蠢的问题,但我需要检查两个时间段(在 epoch 中)是否 重叠 彼此。我只是不知道这些检查是否足够。

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)|

&

|(period.start),(period.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()
   );
}