-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintersection.js
39 lines (35 loc) · 946 Bytes
/
intersection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* @params first {start, end} First interval
* @params second {start, end} Second interval
*/
function thereIsIntersection(a, b) {
if (isEmpty(a) || isEmpty(b)) {
return false;
}
let aStartIsContainedInB = a.start >= b[0] && a[0] <= b[1];
let aEndIsContainedInB = a[1] >= b[0] && a[1] <= b[1];
let aIntervalContainsB = a[0] <= b[0] && a[1] >= b[1];
return aStartIsContainedInB || aEndIsContainedInB || aIntervalContainsB;
}
function isEmpty() {
// TBI
}
describe("interval intersection", function () {
given([
[{ start: 1, end: 6.5 }, { start: 6.2, end: 20 }, true],
// a or b empty
// a and b empty
// a < b
// a > b
// a contains b
// b contains a
// a <= b
// a >= b
]).it("detects two intervals are being intersected", function (
intervalA,
intervalB,
expectedResult
) {
expect(thereIsIntersection(intervalA, intervalB).toEqual(expectedResult));
});
});