I have a polyline defined as an ordered set of coordinates (X, Y) that can intersect itself to form one or more loops. From this I want to extract a polygon in which loops are removed that will include the intersection points where the line intersects itself. I currently have a crude brute force algorithm that works as follows:
int RemoveLoops(CoordType Points[],int NoPoints) { int n = 0; // Start of first line segment int m = 2; // Offset to n of second line segment int p = NoPoints; while (n + m + 1 < p) { CoordType Ip; // Intersection point if (Intersect(Points[n],Points[n+1],Points[n+m],Points[n+m+1],Ip))) { Points[n+1] = Ip; int d = m - 1; // Number of points to delete for (int i = n + m + 1; i < p; i++) Points[i - d] = Points[i]; p -= d; m = 2; continue; // Restart from intersection point } m ++; if (n + m + 1 >= p) // Reached end of line, change starting segment { m = 2; // Reset offset n++; // Increment starting segment } } return(p); // Return the number of points in the new poly line }
While I made several optimizations for the above, for example, counting the cumulative angle between consecutive line segments, we know that we cannot get to the intersection until we go 360 degrees, the algorithm remains pretty terrible O (n ^ 2). I know that I can do much better than this, for example. using the set of all intersecting line segments as a starting point. Given that the items have already been ordered, I suppose I should be able to do better. Please note that the above version works and returns the number of points remaining, which is convenient, but not necessary.
Any ideas on a good algorithm for the above, O (n log n) or better?
source share