Each line segment has 2 xy coordinates given,
Input are below,
[3,4],[5,4] [8,4],[20,4] [10,4],[15,4]In the above picture, if the lines are overlapping, it can be considered as a line segment. May I know the logic or mathematics behind solving it which outputs the 2 non-overlapping line segments which are [3,4],[5,4] and [8,4],[20,4]?
The input I gave is just 3 line segments that we have to filter out to non-overlapping line segments, so this gets complicated fast with more line segments if we don’t have the proper mathematics. I am doing this because I faced this bug in my programming :).
I will appreciate any help that I can obtain 🙂
My solution is I have tried to find whether 2 lines are overlapping from by using some of the logic which is A.start <= B.end && B.start <= A.end
as stated in here. The current code which I am stuck on this problem is published in codepen live demo here.
2
Answers
Your drawing indicates that you’re using two-dimensional data points, with x and y coordinates. Finding overlapping lines in 2D requires more sophisticated logic than just checking start and end points.
However, as your sandbox example is 1D, I’ll address that.
In this solution, you iterate over each of the line segments, sorted by increasing start coordinate. As you encounter each subsequent segment, you choose to either combine the two or add a new segment, depending on whether they overlap or not.
Edit: Here’s an implementation for 2D segments which assumes that all points lie on the same line, and thus only needs to check the x coordinate of each point. A general 2D solution requires more sophisticated logic than this.
I reviewed the code and found there was nothing wrong with the logic. The problem was with the dimensions and input.
Here’s the revised code.