Search code examples
algorithmmathgeometrylineintersection

Line intersection transversal


I have several random line segments. I have to check if there is any intersection between any two line segments. Lines may be connected or not. What would be a good algorithm for this problem?


Solution

  • Assuming you're talking about line segments here (otherwise, simply compare the slopes of the lines: if they have unequal slopes, they intersect).

    To find out if a [single] intersection exists in a set of 2 or more line segments, you can use the Shamos-Hoey algorithm.

    To find all intersections in a set of 2 or more line segments, you can use the Bentley-Ottmann algorithm.

    Implementation of the two, and other "sweep-line" based algorithms, are abundantly available on the internet.