java – 查找Path2D是否自相交

前端之家收集整理的这篇文章主要介绍了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)

原文链接:https://www.f2er.com/java/123739.html

猜你在找的Java相关文章