# Room merging

from Red Blob Games

(Problem described on reddit[1])

You have drawn a room rectangle and place walls around the outside.

Then you draw another room and want walls around the outside. But what happens if the rooms overlap? We want to figure out where the walls should be. Some walls are erased where the rooms meet. First, the demo: drag the mouse to draw a room.

Algorithm:

1. Keep track of what’s a building and what’s outdoor.
2. If a tile is outdoor, draw grass.
3. If a tile is building and one of its neighbors is outdoor, draw a wall.
4. If a tile is building and all of its neighbors are building, draw floor.

Note that in this demo, I place the walls on building tiles. You could also design it to place the walls outside the buildings. In that case, the algorithm becomes:

1. Keep track of what’s a building and what’s outdoor.
2. If a tile is building, draw floor.
3. If a tile is outdoor and one of its neighbors is building, draw a wall.
4. If a tile is outdoor and all of its neighbors are outdoor, draw grass.

(Untested)

Email me , or tweet @redblobgames, or comment: