
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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] ];
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] ];
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.
Example
The code for this will be −
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));
Output
And the output in the console will be −
[ [ 60, 100 ], [ 140, 180 ] ]
- Related Articles
- C++ program to find the number of triangles amongst horizontal and vertical line segments
- How to find all subsets of a set in JavaScript?
- Draw all alphabet having vertical line of symmetry
- Find the number of line segments in the given figure."\n
- How do I find the intersection of two line segments in Matplotlib?
- Klee’s Algorithm (Length Of Union Of Segments of a line) in C++
- Program to find number of sets of k-non-overlapping line segments in Python
- Interval List Intersections in C++
- Can a polygon have intersecting line segments?
- Show that of all line segments drawn from a given point not on it, the perpendicular line segment is the shortest.
- Find all distinct subsets of a given set in C++
- Legend with vertical line in matplotlib
- How to set the vertical alignment of the content in an element with JavaScript?
- How to set the type of line in a text-decoration with JavaScript?
- Maximum possible intersection by moving centers of line segments in C++
