如何检查两条“线”是否重叠?

时间:2019-04-09 19:30:16

标签: javascript 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)|

|(句号开始),(句号结束)|

如何检查它们是否重叠?

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

1 个答案:

答案 0 :(得分:1)

一个相反的问题:它们何时重叠?答案:第一个在第二个结束之后开始,或者第二个在第一个结束之后开始时。所以

TLPeriod.prototype.isOverlapping = function(period) {
   return !(
      period.toPoints().start.getEpoch() > this.toPoints().end.getEpoch() ||
      this.toPoints().start.getEpoch() > period.toPoints().end.getEpoch()
   );
}
相关问题