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?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.