I need to find "holes" in a 2D grid in java - can you point me toward the best sort of algorithm to do this?
With the input of the following points:
5,3
5,4
8,4
5,5
6,3
7,3
7,4
6,5
I need to figure out the positions of the "hole" or surrounded space in this grid. I'm a bit lost as to how to do this.
Plot of the points:
Assuming each point is 1x1
This is basically a blob extraction algorithm + a bit extra. Do this:
1) Find the westmost, eastmost, northmost and southmost any solid is placed. Remember them as xmin xmax ymin ymax.
2) Allocate an 2d array of integers (initialized to 0) with those dimensions, and place all solid points in it as the value -1.
3) Make a counter initialized to 1. Scan the 2d array. Every time you find a point that is 0, set it to
counter
and floodfillcounter
s onto every adjacent point that is not a -1 until you've run out of points to floodfill onto. (To do a floodfill, one way is to keep a set of all points you haven't floodfilled all the neighbours of yet, and iterate over these, adding new points to the set until the set is exhausted -> nothing left to floodfill onto.) Now increment the counter and continue.4) When you've scanned the whole grid, scan the perimeter. Every time you see a non -1 on the perimeter, mark that blob as not being surrounded (by having an array of bools as long as the number of blobs you found).
5) Every numbered blob you have not marked is surrounded.
Read about blob extraction here: http://en.wikipedia.org/wiki/Blob_extraction