Is it possible to efficiently count the number of line segments that overlap a single point P
on a number line?
All the line segments are sitting on a single number line (its a 1-D
world, not a 3-D
world).
Each line segment has a start coordinate X1
and an end coordinate X2
.
Example:
Line segment A spans from X1==1 to X2==3
Line segment B spans from X1==2 to X2==4
Line segment C spans from X1==3 to X2==5
Line segment D spans from X1==1 to X2==4
----------------------------------------
Ex1: Line segments that overlap point P==2: A,B and D >>> overlap count==3.
Ex2: Line segments that overlap point P==7: None >>> overlap count==0.
Ex3: Line segments that overlap point P==3: A,B,C and D >>> overlap count==4.
Of course, if there is only 4 line segments, then the code is simple. However, if there is a huge spatial database of 400 million line segments, then the search is very slow.
Are there any algorithms that can efficiently search this list of line segments for the total number of overlaps?
What I am looking at so far
- Articles on spatial index searching algorithms.
- Interval trees (looks very promising).
- Segment trees (looks very promising).
- RTrees.