I have a collection of rooms, each of which is connected to one or more other rooms via the cardinal directions (north, south, east, west). The rooms have been connected in such a way that if A is west of B, then B is east of A; thus undirected graph. Now I need to take this collection of rooms and graph them on a coordinate plane. All edges must be parallel to the X or Y axis.
I've tried some different approaches, but I think the most effective so far is as follows:
- Find the "center" and assign it (0,0) (room for which the sum of the lengths of the shortest paths to every other room is smallest). I don't know if this is really necessary, but it makes centering the output easier.
- Walk out from the center C and for each room R encountered, assign a coordinate (X, Y), where P is the path connecting C=>R resulting in the greatest displacement on the coordinate plane, X is the net horizontal movement along P, and Y is the net vertical movement along P.
Assume the following vectors for directions: North = [0,1] South = [0,-1] East = [1,0] West = [-1,0]