# Finding an Enclosed Space on a Grid

This topic is 2069 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

What would be a great way to test a random grid based shape to see if it contains a space that needs to be filled in? The spaces can range from a single block to ten blocks. Also the line of blocks has a chance to not form a shape but just form a line.

Behemyth

##### Share on other sites
It's not very clear what you are after, but this might be helpful:
[url="http://en.wikipedia.org/wiki/Flood_fill"]http://en.wikipedia....wiki/Flood_fill[/url] Edited by szecs

##### Share on other sites
[quote name='Behemyth' timestamp='1337352632' post='4941188']
to see if it contains a space that needs to be filled in
[/quote]

That bit is unclear to me. What do you mean by a space that needs to be filled in? The "need" may depend upon the game rules. Also does this need to be recalculated based upon user actions? For example, can a player open up an enclosed space (e.g. join it to another space), or can a player break an area into two areas? Do you need to be able to identify which grid cell belongs to which enclosed area quickly?

##### Share on other sites
To clear things up, the random shape that is placed on screen is not always solid. It sometimes has holes in it and I would need to have a way to fill those holes. Also, It would have to recognize what is a shape and only apply filling to that shape.

##### Share on other sites
Create a blank grid with each block set to a given value, say 0. Then place your object on the grid so that each block in your object has a different value, say 1. Then flood fill the outside of your grid with a different value, say 2. Whatever is left as 0 is a hole on your object.

##### Share on other sites
Ah okay, easy. I assume you know the rectangle that encloses your shape. For each row in that rectangle find the furthest left filled point and furthest rght filled point, and fill the line between these two points. Do the same rotated 90 degress, e.g. lines from top to bottom.

##### Share on other sites
That may fill in concavities that are not enclosed by the shape. Whether or not that is what the OP desires is unclear given the description.

##### Share on other sites
It depends what you mean by enclosed. It would fill in gaps that have a diagonal path to the outside. I assumed Manhattan and maximal filling.

##### Share on other sites
It would also fill in the mouth of a Pacman shape, which isn't enclosed by pretty much any definition.

##### Share on other sites
[quote name='SiCrane' timestamp='1337453317' post='4941488']
Create a blank grid with each block set to a given value, say 0. Then place your object on the grid so that each block in your object has a different value, say 1. Then flood fill the outside of your grid with a different value, say 2. Whatever is left as 0 is a hole on your object.
[/quote]

That is a pretty spiffy idea! Thanks to all who contributed.

##### Share on other sites
[quote name='SiCrane' timestamp='1337514549' post='4941623']
It would also fill in the mouth of a Pacman shape, which isn't enclosed by pretty much any definition.
[/quote]