该算法假设线段不是垂直的,线段端点不位于其他线段上,交叉点由仅两条线段形成,并且没有两个事件点具有相同的x坐标。然而,这些一般位置假设对于大多数线段交点应用程序来说并不合理。
我的问题是,是否有一种通用化的算法可以克服/解决上述困难?
The algorithm proceeds as follows: It first creates events for all start- and endpoints of input segments. It then handles all events in sorted order.
For each event, it fetches the associated list L of segments starting at that point and finds and removes all segments in the search tree that intersect the current event point. It reports all those segments as intersections in that point. It then switches the order of the comparison function by changing the flag to be slightly after the current event. It reinserts all previously removed segments that do not have their endpoint at the current event point (because they have to be removed from the structure at this point anyways) and additionally insert the segments from L (that are starting at this point). It checks the top and bottom neighbors above and below the current event point for new intersections and adds them as event points if it finds any.
This way the algorithm is robust against all kinds of degeneracies, including vertical segments and overlapping segments, as well as segments intersecting on their endpoints.
for all segments s do Create events for the endpoints of s end for while event queue not empty do Remove the smallest event point p from the queue Let L be the set of segments that start at p I ← all segments in the sweep line structure that contain p remove I from sweep line structure if |L| + |I| ≥ 2 then Report intersections of L ∪ I end if C ← {s ∈ I | p is not endpoint of s} Insert C in sweep line structure in reversed order Check for new intersections among segments above and below p end while