Finding an Enclosed Space on a Grid

Started by
9 comments, last by jefferytitan 11 years, 11 months ago
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
Advertisement
It's not very clear what you are after, but this might be helpful:
http://en.wikipedia....wiki/Flood_fill

to see if it contains a space that needs to be filled in


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?
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.
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.
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.
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.
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.
It would also fill in the mouth of a Pacman shape, which isn't enclosed by pretty much any definition.

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.


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

This topic is closed to new replies.

Advertisement