解决方法
您可以使用扫描线算法:
http://en.wikipedia.org/wiki/Sweep_line_algorithm更快地执行此操作
伪代码:
Each line has a start point and an end point. Say that `start_x` <= `end_x` for all the lines. Create an empty bucket of lines. Sort all the points by their x coordinates,and then iterate through the sorted list. If the current point is a start point,test its line against all the lines in the bucket,and then add its line to the bucket. if the current point is an end point,remove its line from the bucket.
最坏的情况仍然是O(N ^ 2),但平均情况是O(NlogN)