0

Let L1,...,Ln be n different line segments in the plane (IR^2). They shall be pairwise non-intersecting. Furthermore let r signify a distance (a real value). Consider the problem of finding all pairs (i,j) where the (euclidean) distance of Li and Lj is less than r.

I wrote a simple and straightforward sweep-line algorithm for the problem which is about of runtime O(n^(3/2)), if one can assume that for all relevant x0 coordinates about n^(1/2) line segments lie in the vertical stripe bounded by x = x0 and x = x0 + r.

Of course I was curious, if there is a well-known (or not so well-known) better algorithm (hopefully a O(n log(n)) algorithm or such), but could not find anything appropriate via google or more specifically stackoverflow.

Does someone know more?

Jürgen Böhm
  • 389
  • 3
  • 11

1 Answers1

1

If you replace the segments by rectangles corresponding to a dilation by r/2, the rectangle outlines will intersect when the segments are closer than r. (In fact, there will be a fraction of false positives, because the corners should be rounded. But the false positives can be rejected after the fact.)

So you could use the standard Bentley-Ottman to perform your task, with no degradation of the asymptotic complexity. (Note that two rectangle outlines can intersect up to eight times, but only in extreme situations.)

https://en.wikipedia.org/wiki/Bentley–Ottmann_algorithm

enter image description here

  • Really a great answer! I will implement it during the next days. In fact, I just noted that there is already an implementation of the sweep-line for curves in CGAL: https://doc.cgal.org/latest/Surface_sweep_2/index.html#Chapter_2D_Intersection_of_Curves – Jürgen Böhm Apr 14 '20 at 20:11
  • @JürgenBöhm: isn't the CGAL algorithm overkill ? It can compute arrangements of curves, a much more general problem. –  Apr 14 '20 at 20:29
  • Yes, and it necessitates seemingly an exact number field kernel, which will probably slow things down quite a bit compared to a kernel working on "double" numbers. Maybe it is better to develop it from scratch - the generic case is obviously quite easily done with C++ and STL, but I am a bit afraid of the boundary cases where the segments are in "degenerate" position. – Jürgen Böhm Apr 14 '20 at 20:35
  • @JürgenBöhm: if, due to degenerate positions you miss an intersection, think that you may have a "second chance" because two closed contours intersect in an even number of points. For instance, if two sides are parallel, there can be another intersection with two orthogonal sides. –  Apr 15 '20 at 06:37