简体   繁体   English

查找一组线段中的所有交点?

[英]Finding all intersection points in a group of line segments?

I have an array of objects (Lines) and a binary operation (intersection) that returns true/false.我有一个对象数组(线)和一个返回真/假的二元运算(交集)。 The brute force is to duplicate the array an run on a nested for loop.蛮力是复制数组并在嵌套的 for 循环上运行。

for (Line line : linesArray) {
            for (int j = 0; j < linesArray.length; j++) {
                if (line.isIntersecting(linesArray[j])) {
                    Point intersection = line.intersectionWith(linesArray[j]);
                    d.drawCircle(intersection.getXInt(), intersection.getYInt(), 3);
                    d.fillCircle(intersection.getXInt(), intersection.getYInt(), 3);
                }
            }
        }

But, there order does not matter, so I thought about generating a subsets of size 2 and check for each subset, but complex wise it does not seems to be any better但是,顺序并不重要,所以我考虑生成一个大小为 2 的子集并检查每个子集,但复杂的明智之举似乎并没有更好

Is there an efficient (run time) to do so?这样做是否有效率(运行时间)?

You are correct that enumerating all subsets of size two and checking each will not be an improvement over your existing algorithm.您是正确的,枚举所有大小为 2 的子集并检查每个子集不会对您现有的算法有所改进。 In fact, that's essentially just a restatement of what your algorithm does.事实上,这本质上只是对你的算法所做的重述。

There are faster algorithms for solving the problem of finding all intersections in a collection of line segments.有更快的算法可以解决在一组线段中查找所有交点的问题。 The most famous of these is theBentley-Ottmann algorithm , which works by sorting the line segments by their x coordinates and sweeping a line over the points from left to right.其中最著名的是Bentley-Ottmann 算法,它的工作原理是按线段的 x 坐标对线段进行排序,并在点上从左到右扫过一条线。 It works in time O((n + k)log n), where n is the number of line segments and k is the number of intersections.它的工作时间为 O((n + k)log n),其中 n 是线段数,k 是交点数。

Other, more modern algorithms exist for solving this problem even faster, but this would likely make for a good starting point.其他更现代的算法可以更快地解决这个问题,但这可能是一个很好的起点。

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM