2016-11-12 127 views
2

我想實現一個函數,當給出兩個代表行的對象時,返回它們是否重疊。檢查範圍重疊

這是它應該如何看起來像視覺。

實施例1:

checkOverlap({start: 0, end: 10}, {start: 8, end: 15}) 

在視覺上,將是:

0--------10 
    8-------15 
    ^overlap 

返回true

實施例2:

checkOverlap({start: 12, end: 15}, {start: 0, end: 10}) 

在視覺上,將是:

   12-------15 
0--------10 

       no overlap 

返回false

這裏是我的功能,適用於一些但不是所有:

function checkOverlap(lineA, lineB) { 
    var result; 
    for(var a in lineA) { 
     for(var b in lineB) { 
      if(a.end > b.start) { 
       result = true; 
      } else { 
       result = true; 
      } 
     } 
    } 
    return result; 
} 
+0

* 「它適用於一些但不是所有的」 *爲了什麼輸入不工作?從我可以告訴的是,它不應該工作。做一個'console.log(a,b)'看看'a'和'b'的值究竟是什麼。另外,爲什麼你將相同的值賦給'if'和'else'塊中的'result'?這意味着'result'將總是*爲'true'。你認爲「(A行中的變量)」是什麼意思?你爲什麼使用循環? –

+0

對不起,它不工作,它只是輸出真,其餘的代碼不工作 – sal

回答

2

尼娜·肖爾茨答案是行不通的,如。 a = {start:1,end:2},b = {start:0,end:10}。

如果線{開始:0,結束:10}和{啓動:10,端:15}被計算爲重疊:

function checkOverlap(lineA, lineB) { 
     return lineA.start >= lineB.start && lineA.start <= lineB.end || 
       lineA.end >= lineB.start && lineA.end <= lineB.end || 
       lineB.start >= lineA.start && lineB.start <= lineA.end || 
       lineB.end >= lineA.start && lineB.end <= lineA.end; 
    } 

如果不是:

function checkOverlap(lineA, lineB) { 
     return lineA.start > lineB.start && lineA.start < lineB.end || 
       lineA.end > lineB.start && lineA.end < lineB.end || 
       lineB.start > lineA.start && lineB.start < lineA.end || 
       lineB.end > lineA.start && lineB.end < lineA.end; 
    } 
+0

這不會工作在這個var lineA = {start:0,end:10}; var lineB = {start:10,end:20}; – sal

+0

在你的情況下,它應該算作重疊還是不重疊?如果不在所有情況下刪除=登錄 – pato

+0

並非所有情況下,某些應該等於true並且有一些爲假 – sal

2

你可以覈對對象的邊界。

function checkOverlap(o1, o2) { 
 
    return (
 
     o1.start >= o2.start && o1.start <= o2.end || 
 
     o1.end >= o2.start && o1.end <= o2.end || 
 
     o2.start >= o1.start && o2.start <= o1.end || 
 
     o2.end >= o1.start && o2.end <= o1.end 
 
    ); 
 
} 
 

 
console.log(checkOverlap({start: 0, end: 10}, {start: 8, end: 15})); // true 
 
console.log(checkOverlap({start: 8, end: 15}, {start: 0, end: 10})); // true 
 
console.log(checkOverlap({start: 12, end: 15}, {start: 0, end: 10})); // false 
 
console.log(checkOverlap({start: 0, end: 10}, {start: 12, end: 15})); // false 
 
console.log(checkOverlap({start: 12, end: 15}, {start: 16, end: 17})); // false 
 
console.log(checkOverlap({start: 16, end: 17}, {start: 12, end: 15})); // false 
 
console.log(checkOverlap({start: 1, end: 2}, {start: 0, end: 10})); // true 
 
console.log(checkOverlap({start: 0, end: 10}, {start: 1, end: 2})); // true 
 

 
console.log(checkOverlap({start: 0, end: 10}, {start: 10, end: 20})); // true 
 
console.log(checkOverlap({start: 10, end: 20}, {start: 0, end: 10})); // true
.as-console-wrapper { max-height: 100% !important; top: 0; }

+0

它不會工作在這個var lineA = {start:0,end:10}; var lineB = {start:10,end:20};或者這個var lineA = {start:10,end:20}; var lineB = {start:0,end:10}; – sal

+1

@sal,返回'true'。那是不對的? –

0

重疊必須遵循兩個條件

![enter image description here

  • o1.end - o2.start > 0 // >= 0 if 0-10 10-20 means overlapping

  • o2.end - o1.start > 0 // >= 0 if 10-20 0-10 means overlapping

function checkOverlap(o1, o2) { 
 
    return ((o1.end - o2.start) > 0 && (o2.end - o1.start) > 0) ? true : false; 
 
} 
 

 
console.log(checkOverlap({start: -10, end: 0}, {start: 0, end: 10})); // false 
 
console.log(checkOverlap({start: -20, end: -10}, {start: -5, end: 5})); // false 
 
console.log(checkOverlap({start: 5, end: 10}, {start: 10, end: 20})); // false 
 
console.log(checkOverlap({start: -10, end: 0}, {start: -5, end: 5})); // true 
 
console.log(checkOverlap({start: -5, end: 5}, {start: -10, end: 0})); // true 
 
console.log(checkOverlap({start: 0, end: 10}, {start: 5, end: 15})); // true 
 
console.log(checkOverlap({start: 5, end: 15}, {start: 0, end: 10})); // true
.as-console-wrapper { max-height: 100% !important; top: 0; }