I’m working on a 2D top down map generation algorithm where all I’m creating are rooms that have a Point for top/left and a Point for bottom/right. All rooms are rectangular and do not overlap on any part except some occasionally share walls.
My question is, when I have two rooms that end up sharing a wall between them, how would I quickly and easily spit out a list of ’tiles’ (or Points) that are shared by both rooms within said wall?
I’m kind of hoping for something like:
private List<Point> SharedTiles (Point P1, Point P2, Point P3, Point P4)
{
/*
P1 = Top Left point of room1
P2 = Bottom Right point of room1
P3 = Top Left point of room2
P4 = Bottom Right point of room2
*/
List<Point> _SharedTiles = new List<Point>();
//Magic goes here...
return _SharedTiles;
}
Given two rooms, A and B:
I am assuming that the points specified do not include the walls of the room. If this is incorrect, you can skip the rectangle inflation in the first step.
Represent each room as a
Rectanglethat is extended one tile in each direction to account for the walls:Intersect the room areas:
The resulting rectangle will represent the intersection between the areas of the two rooms. Since you’ve stated that rooms can only share wall tiles, it necessarily follows that all of the tiles in this intersection area will be wall tiles.
If necessary, you can convert this rectangle into a list of
Pointobjects. I would not necessarily recommend the method below, depending on how this is being used, but it communicates the point: