Public
Edited
Apr 20, 2023
Importers
1 star
Insert cell
Insert cell
function testOverlap(range1, range2) {
if (range1[1] <= range2[0] || range1[0] >= range2[1]) {
// No overlap
return null;
} else if (range1[0] <= range2[0] && range1[1] >= range2[1]) {
// Complete coverage of range2 by range1
const overlapIndices = [range2[0], range2[1]];
const overlapInR1 = [
overlapIndices[0] - range1[0],
overlapIndices[1] - range1[0]
];
const overlapInR2 = [0, overlapIndices[1] - range2[0]];
return {
type: "Coverage",
ranges: [
{ range: range1, overlap: overlapInR1 },
{ range: range2, overlap: overlapInR2 }
]
};
} else if (range1[0] >= range2[0] && range1[1] <= range2[1]) {
// Complete coverage of range1 by range2
const overlapIndices = [range1[0], range1[1]];
const overlapInR1 = [0, overlapIndices[1] - range1[0]];
const overlapInR2 = [
overlapIndices[0] - range2[0],
overlapIndices[1] - range2[0]
];
return {
type: "Coverage",
ranges: [
{ range: range2, overlap: overlapInR2 },
{ range: range1, overlap: overlapInR1 }
]
};
} else if (range1[1] >= range2[0] && range1[0] < range2[0]) {
// Overlap on the left of range2
const overlapIndices = [range2[0], range1[1]];
const overlapInR1 = [
overlapIndices[0] - range1[0],
overlapIndices[1] - range1[0]
];
const overlapInR2 = [0, overlapIndices[1] - range2[0]];
return {
type: "Overlap",
ranges: [
{ range: range1, overlap: overlapInR1 },
{ range: range2, overlap: overlapInR2 }
]
};
} else if (range1[0] <= range2[1] && range1[1] > range2[1]) {
// Overlap on the right of range2
const overlapIndices = [range1[0], range2[1]];
const overlapInR1 = [
overlapIndices[0] - range1[0],
overlapIndices[1] - range1[0]
];
const overlapInR2 = [overlapIndices[0] - range2[0], range2[1] - range2[0]];
return {
type: "Overlap",
ranges: [
{ range: range1, overlap: overlapInR1 },
{ range: range2, overlap: overlapInR2 }
]
};
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more