java – 查找Path2D是否自相交

我需要找到Path2D是否相交.现在,我通过简单地从路径中提取一行数组,并发现这些行中是否有相交.但是它具有O(n ^ 2)的复杂性,所以非常慢.有更快的方法吗?

解决方法

您可以使用扫描线算法: 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)

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...