Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Find all disjointed intersections in a set of vertical line segments in JavaScript
We have a set of vertical regions defined by y1 and y2 coordinates, where y1 is the starting point and y2 is the ending point of each region.
The origin of our coordinates system is the top-left corner, so y2 is always greater than y1.
This is an example:
const regions = [
[10, 100],
[50, 120],
[60, 180],
[140, 220]
];
console.log(regions);
[ [ 10, 100 ], [ 50, 120 ], [ 60, 180 ], [ 140, 220 ] ]
We are required to write a JavaScript function that takes in one such region array as the first argument and a number as the second argument.
We would like to find out all disjointed intersections greater than a certain size, (specified by the second argument of the function).
Let's say, for example, 20 units.
Then the output for the above array should look like:
const output = [
[60, 100],
[140, 180]
];
How It Works
We could use a simplified algorithm and use across the product for searching for overlapping items. Then get the common parts by iterating and filter only unknown matches.
Algorithm Implementation
The algorithm finds intersections by comparing each region with others and calculating overlapping segments that meet the minimum size requirement:
const regions = [
[10, 100],
[50, 120],
[60, 180],
[140, 220]
];
const getIntersections = (arr, num) => {
let disjoint, res;
return arr.reduce((acc, val, ind, array) => {
if (val.used) {
return acc;
}
res = array.map((el, index) => array[(ind + index) % array.length])
.reduce((s, e) => {
disjoint = [Math.max(s[0], e[0]), Math.min(s[1], e[1])];
return disjoint[0] < disjoint[1] ? (e.used = true, disjoint) : s;
});
res[1] - res[0] > num && acc.push(res);
return acc;
}, []);
}
console.log(getIntersections(regions, 20));
[ [ 60, 100 ], [ 140, 180 ] ]
Step-by-Step Breakdown
Let's break down how the algorithm works with a simpler example:
// Function to find intersection between two segments
function findIntersection(segment1, segment2) {
const start = Math.max(segment1[0], segment2[0]);
const end = Math.min(segment1[1], segment2[1]);
// Return intersection if it exists, null otherwise
return start < end ? [start, end] : null;
}
// Test intersection logic
const seg1 = [10, 100];
const seg2 = [60, 180];
const intersection = findIntersection(seg1, seg2);
console.log(`Segment 1: [${seg1}]`);
console.log(`Segment 2: [${seg2}]`);
console.log(`Intersection: [${intersection}]`);
console.log(`Size: ${intersection[1] - intersection[0]} units`);
Segment 1: [10,100] Segment 2: [60,180] Intersection: [60,100] Size: 40 units
Key Points
- The algorithm uses
Math.max()to find the intersection start point - It uses
Math.min()to find the intersection end point - Only intersections larger than the specified threshold are included
- The
usedflag prevents duplicate processing of segments
Conclusion
This algorithm efficiently finds all disjointed intersections in vertical line segments by comparing overlapping regions and filtering results based on minimum size requirements. The approach ensures each intersection is counted only once.
