简体   繁体   English

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

[英]How to check if two 'lines' overlap?

I know, its a dumb question, but I need to check if two periods of time (in epoch ) overlap with each other. 我知道,这是一个愚蠢的问题,但我需要检查两个时期(以epoch )是否相互重叠 I just don't know if those checks will be sufficient. 我只是不知道这些检查是否足够。

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;
};

I know, i should write here , but it would take lots of time to get an answer. 我知道,我应该在这里写,但是要花很多时间才能得到答案。

It could be quickly summarized to: 可以快速总结为:

Two lines on the same axis with points: 同一点上两条线与点:

|(this.start),(this.end)| |(this.start),(this.end)|

&

|(period.start),(period.end)| |(period.start),(period.end)|

How to check if they overlap? 如何检查它们是否重叠?

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

An opposite question: when do they not overlap? 一个相反的问题:它们什么时候重叠? The answer: when the first one starts after the second one ends, or when the second one starts after the first one ends. 答案:第一个在第二个结束之后开始,或者第二个在第一个结束之后开始时。 So 所以

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

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

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