Seeking a method to divide two overlapping ranges when they intersect.
This is my current progress using typescript,
type Range = {
start: number;
end: number;
};
function splitOverlap(a: Range, b: Range): Range[][] {
let result = [];
const intersect = {
start: Math.max(a.start, b.start),
end: Math.min(a.end, b.end),
};
let a1: Range;
let a2: Range;
if (a.start >= intersect.start) {
a1 = {
start: a.start,
end: Math.min(a.end, intersect.end),
};
a2 = {
start: Math.max(a.start, intersect.start),
end: Math.min(a.end, intersect.end),
};
} else if (a.start < intersect.start) {
a1 = {
start: a.start,
end: Math.min(a.end, intersect.end),
};
a2 = {
start: intersect.start,
end: Math.min(a.end),
};
}
return [
[a1, a2],
[b, b],
];
}
/*
Input
A |-------------|
B |-------------|
Output
A |-------|------|
B |------|------|
Input
A |-------------|
B |-------|
Output
A |--|-------|--|
B |-------|
*/
I am not tasked with merging, only splitting when overlap occurs.
My split function first determines the intersection between the ranges, then attempts to split the input ranges utilizing this intersect data. I'm unsure of the most effective approach.