# How to get enclosed spaces from a series of connected nodes

I have a bunch of connected walls in a list and the data for them is like so:

Wall
{
Node A;
Node B;
}

Node
{
float x;
float y;
}


I want to find the rooms from the connected walls as an array of connected points to represent each room's perimeter.

This is an example visually of what i am trying to find:

The red dots are the nodes, and the lines are the walls, the numbers are the identified rooms that the walls created.

The walls can be at any angle, not sure if that matters though.

I am wondering what algorithms exist that can help me solve this problem, what is the best way to approach this?

Alternatively, you could solve your problem directly. For each node, find all of the walls associated with it, sort them by their angle, and store that sorted list associated the node. After doing that for all nodes, then you can iterate through all rooms. Pick an wall, then you can find the room to the "right" of that wall by simulating the left-hand rule: stand to the right of that wall, put your left hand on the wall, and walk forward, going in a circle around the perimeter of the room. To simulate that rule, as you walk forward, you'll walk to the endpoint of the current wall; at that node $$d$$, to find the next wall you proceed to, look in the sorted list of walls incident on $$d$$, and find the next one in sorted order, then follow that wall. It might be a bit trickier to work out the details of this, than to use an existing implementation of a DCEL data structure.