Skip to main content
GameDev.net gamedev.net
🔒 Locked

Finding an Enclosed Space on a Grid

Started by Behemyth May 18, 2012 at 2:50 PM 9 replies 4.2k views
Original Post
Behemyth
Behemyth
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
jefferytitan
jefferytitan

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

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.
jefferytitan
jefferytitan

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


Oops, my bad. ;)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.