Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect all regions that are surrounded by n line segments?

When you draw 3 line segments in a 2-dimensional plane, it might compose a triangle.

How can I find all polygons that are produced by n line segments? Are there any efficient algorithms I can use?

Input: first and last point coordinate for each line segments (Ex. Points A=(x_A,y_A), B=(x_B,y_B), ... , I=(x_I,y_I))

lines

Output: All produced polygons and producing line sets (Ex. {A,B,C,F},{A,C,E,F,H},{E,F,I},{E,F,I,H},{G,H,I})

polygons produced by given lines

like image 951
Izumi Kawashima Avatar asked Dec 30 '25 09:12

Izumi Kawashima


1 Answers

I found the answer.

Step 1. Calculate all intersecting points for each line segments.

Referring "How do you detect where two line segments intersect?", calculate all intersecting points for the given line segments. It's O(n^2), but can be upgraded to O(n log n) by using spatial trees (ex. R-Tree, Quad trees).

enter image description here

Step 2. Find all couter-clockwise loops.

Referring "small cycle finding in a planar graph", Calculate the connecting edge angle for each vertex, and sort it. After it's done, traverse every edges and find all loops by "turning to the most-left-edge" strategy.

This will find all loops, but will also find an outer loop that's not needed. The outer loop is clockwise, compared to other loops that are all counter-clockwise, so remove the clockwise loop by using method written in "How to determine if a list of polygon points are in clockwise order?".

enter image description here

like image 159
Izumi Kawashima Avatar answered Jan 04 '26 13:01

Izumi Kawashima



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!