我正在尝试实现一个函数,当给定两个表示线条的对象时,它会返回它们是否重叠。
以下是视觉效果。
示例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 :(得分:3)
Nina Scholz的回答不起作用,例如。 a = {start:1,end:2},b = {start:0,end:10}。
如果行{start:0,end:10}和{start:10,end: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;
}
答案 1 :(得分: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
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
答案 2 :(得分:0)
重叠必须遵循两个条件:
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; }