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

标签 javascript overlap

我知道,这是一个愚蠢的问题,但我需要检查两个时间段(以纪元为单位)是否相互重叠。我只是不知道这些检查是否足够。

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/

相关文章:

javascript - JavaScript 函数方法

javascript - 水平显示卡片列表,带有 Angular 和 MaterializeCSS 的 HTML/CSS

javascript - 使用 jquery 处理 CSV

mysql - 如何测试某个时间段是否与某个时间段有重叠?

java - 如何使用 Simmetrics java 计算重叠系数和 Jaro Winkler

c# - 拆分重叠日期范围的最快方法

javascript - SQL node.js 看似有效的查询存在语法错误?

javascript - 在 angular.js 中过滤 $routeParams

确定一个矩形是否完全被另一组矩形覆盖所需的算法

css - 重叠 2 个 DIV 以显示颜色混合