2

As input we only have a set of segments, here is an example :

[AB] = [(0, 0), (0, 4)]    ;    [BC] = [(0, 4), (2, 6)]

[CD] = [(2, 6), (4, 0)]    ;    [DA] = [(4, 0), (0, 0)]

[CE] = [(2, 6), (5, 6)]    ;    [EF] = [(5, 6), (5, 3)]

[FG] = [(5, 3), (3, 3)]    ;    [HI] = [(2, 1), (4, 5)]

[JK] = [(1, 3), (2, 3)]    ;    [KL] = [(2, 3), (1, 1)]

[LJ] = [(1, 1), (1, 3)]

(sorry i tried my best but my images are a bit blurred)

enter image description here

I need to find all the individual areas. From the figure above i would have 3 different areas, JKL, CEFG and {ABCD, JKL}:

enter image description hereenter image description hereenter image description here

Note that segments which don't make any area are ignored (such as [HI]), and an area cannot be split by a segment, e.g:

enter image description hereenter image description here

I could do it easily with some spaghetti code which would be totally unefficient, but before doing it, do you have any idea of an algorithm i can start working on? i'm looking for something as efficient as possible...

Cinn
  • 4,281
  • 2
  • 20
  • 32
  • Is it possible that lines intersect, say e.g. if H was below AD and so GH intersects AD ? – m69's been on strike for years Aug 16 '17 at 23:22
  • Find all the connected graph – Hariom Singh Aug 17 '17 at 02:30
  • What do you mean by "cannot be split by a segment"? – Surt Aug 17 '17 at 05:34
  • I have several segments as input, and these segments create some polygons by crossing each other. My problem is to get the areas shaped by those polygons. @m69 if lines intersect it creates a new point (intersection of [HG] and [AD]). @ Singh definitly not a stand-alone solution but might be a good start, i will work on it... @ Surt i mean that if an area is divided by a segment i individually keep the 2 subareas instead of one – Cinn Aug 17 '17 at 08:44

1 Answers1

0

Idea: Trying to create the smallest rings from the least connected nodes. Reducing the problem size as we go.

  1. Remove all nodes with only one segment as they can't be part of any area.
  2. Sort the nodes in ascending order after number of segments (you will want a O(n log log n) or less algorithm)
  3. Select the node with the fewest segments
  4. flood fill along segments until a potential target is already filled (don't go backward)
  5. ... (there could be 2 plus rings completed here)
  6. profit by removing 2 segment nodes that are connected to the selected node
  7. update the remaining nodes segments.

ToDo: what if the least connected node is 3 or higher ...

Surt
  • 15,501
  • 3
  • 23
  • 39
  • well seen for the first step! i think it's a good start, we can even improve it a bit by looping over this first step until no more segment can be removed... however flood filling does not seem to be a good choice here, since we are working with vectorial elements and not a raster images... – Cinn Aug 17 '17 at 21:08
  • The idea is the same as in Dijkstra, examine the closed edge to the current set. – Surt Aug 17 '17 at 21:10