I am looking for an algorithm that can find separate rooms inside a set of walls, by determining which openings should be considered doorways between adjacent rooms. All is defined on a grid. My main problem is that I cannot be sure that both sides of the same passage will be in one straight line.
In the picture there is an example with five rooms separated with red lines and black walls, although it might be acceptable to merge the middle room with one of the others. I have the black walls as input, and I want to generate the red lines marking the doorways as the output.
Basically I am trying to avoid having to mark them manually to minimize any chance of making a mistake. I made some attempts with focusing on the corners, but got stuck with the passageways not in a straight line problem.
Any ideas?
The walls are stored in a two dimensional array of (half-)bytes storing a bitmask where
- 1 means that the wall in this cell has a connection to the cell above
- 2 -> connection to the cell to the right
- 4 -> connection to the cell below
- 8 -> connection to the cell to the left
- 0 -> cell is empty room
If the wall in a cell has multiple connections the sum of the connections is stored. For example a '3' would represent a "L" shaped wall segment, 'E'(14 in hex) would represent a "T" shape and 'F'(15) would represent a "+" shape.
The example would be stored like this:
6AAEAAC
5001005
5000005
780002D
5000005
5005005
3AA9029