SO,
The problem
I have a question about algorithm of determining if two points are connected on 2D-plane. I have:
- Array of 2D-lines. Each line is limited with its start end end 2D-point. Each point is simple array of two elements
[x,y]
- i.e. each line looks like['start'=>[X0, Y0], 'end'=>[X1, Y1]]
Let this lines set be named asL
. Lines can belongs one to another (i.e. one could be part of another), they can be intersected by only one point e.t.c. - i.e. there are no restrictions for them on 2D plane. But line can not be a point - i.e. start of line can not be equal to end of line. - Two points
S
andE
, i.e. arrays[Xs, Ys]
and[Xe, Ye]
Now, all lines from L
are drawn on the plane. Then, S
and E
are also drawn and I need to answer the question - can E be reached from S without intersection of any lines in L? And, to be more specific - which algorithm will be optimal? By 'could be reached' I mean that there is a way on the plane from S
to E
without intersection any line from L
- and, of cause, this way could be anything, not just line.
Sample
-as you see, in sample S
and E
are not connected. Also in the sample there is a case when one line fully belongs to another (gray and purple lines) - and a case when one line has a start/end on another line (green and rose lines).
My approach
Currently, I have non-deterministic polynomial (NP) algorithm to do that. It's steps are:
- Find all intersections for each pairs of lines.
- Create new set of lines from points of first step. If two lines has one intersection, then there will be 4 new lines with intersection point at the start of each new line - or it can be 3 new lines if first line has it's start/end on second line - or it can be 2 new lines if first line has it's start/end exactly match with second's line start/end. I.e:
so 1-st case will result in 4 new lines, 2-4 cases in 3 new lines and 5 case in 2 new lines. (lines are [A, B]
and [C, D]
)
- Next, in lines step from 2-nd step I'm searching all polygons. Polygon is a closed line set (i.e. it holds closed part of area)
- I'm determining for
S
subset of such polygons that containS
. To do this, I'm using simple algorithm - counting number of intersections with polygon's edges and some line fromS
to outer plane (if it's odd, thenS
is inside polygon and if it's even - then outside). This is a ray-casting algorithm. Then I do that forE
- Now, when I have polygons set for both
S
andE
I'm simply comparing both sets. If they are equal, thenE
could be reached fromS
and could not be - otherwise.
Why is this NP?
The answer is simple: on 3-rd step I'm performing search of all loops in 2D-graph. Since a problem of finding maximum/minimum loop length if NP, then mine is too (because I can get maximum/minimum loop length simply with sorting resulted loops set). Good information about this is located here.
The question
Since my current solution is NP, I want to know - may be my solution for the problem is an overkill? I.e. may be there are simpler solution which will result in polynomial time?