Created
February 18, 2019 16:42
-
-
Save belichuk/75e8916bf0a1e707d4b72fa23d74f434 to your computer and use it in GitHub Desktop.
Overlap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// input: | |
// 0 4 9 10 | |
// <-----------|-------------------------|----------------> | |
// true false true | |
// [[0,4,true],[4,9,false],[9,10,true]] | |
// 0 6 10 | |
// <-----------------------|------------------------------> | |
// true false | |
// [[0,6,true],[6,10,false]] | |
// output: | |
// 0 4 6 9 10 | |
// <-----------|-----------|-------------|----------------> | |
// true false false false | |
const multiRange = (range1, range2) => { | |
var output = []; | |
for (var i = j = 0, out; i < range1.length && j < range2.length;) { | |
out = [ | |
Math.max(range1[i][0], range2[j][0]), | |
Math.min(range1[i][1], range2[j][1]), | |
range1[i][2] && range2[j][2] | |
]; | |
out[1] === range1[i][1] ? i++ : j++; | |
output.push(out); | |
} | |
return output; | |
} | |
const output = multiRange([[0,4,true],[4,9,false],[9,10,true]], [[0,6,true],[6,10,false]]); | |
console.log(output); | |
/* | |
[ [ 0, 4, true ], | |
[ 4, 6, false ], | |
[ 6, 9, false ], | |
[ 9, 10, false ] ] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment