• Create Account

## Finding an Enclosed Space on a Grid

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

10 replies to this topic

### #1Behemyth  Members

233
Like
0Likes
Like

Posted 18 May 2012 - 08:50 AM

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

### #2szecs  Members

2824
Like
0Likes
Like

Posted 18 May 2012 - 09:18 AM

It's not very clear what you are after, but this might be helpful:
http://en.wikipedia....wiki/Flood_fill

Edited by szecs, 18 May 2012 - 09:19 AM.

### #3jefferytitan  Members

2508
Like
0Likes
Like

Posted 18 May 2012 - 03:38 PM

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?

### #4Behemyth  Members

233
Like
1Likes
Like

Posted 19 May 2012 - 12:39 PM

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.

### #5SiCrane  Moderators

11529
Like
1Likes
Like

Posted 19 May 2012 - 12:48 PM

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.

### #6jefferytitan  Members

2508
Like
0Likes
Like

Posted 19 May 2012 - 06:02 PM

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.

### #7SiCrane  Moderators

11529
Like
0Likes
Like

Posted 19 May 2012 - 07:13 PM

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.

### #8jefferytitan  Members

2508
Like
0Likes
Like

Posted 20 May 2012 - 01:59 AM

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.

### #9SiCrane  Moderators

11529
Like
0Likes
Like

Posted 20 May 2012 - 05:49 AM

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

### #10Behemyth  Members

233
Like
0Likes
Like

Posted 20 May 2012 - 10:22 AM

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.

### #11jefferytitan  Members

2508
Like
0Likes
Like

Posted 20 May 2012 - 03:24 PM

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