Question

I have an array - 2D(100 x 100 in this case) with some states limited within borders as shown on picture: http://tinypic.com/view.php?pic=mimiw5&s=5#.UkK8WIamiBI

Each cell has its own id(color, for example green is id=1) and flag isBorder(marked as white on pic if true). What I am trying to do is exclude set of cell with one state limited with borders(Grain) so i could work on each grain separately which means i would need to store all indexes for each grain.

Any one got an idea how to solve it?

Was it helpful?

Solution

Now that I've read your question again... The algorithm is essentially the same as filling the contiguous area with color. The most common way to do it is a BFS algorithm.

Simply start within some point you are sure lays inside the current area, then gradually move in every direction, selecting traversed fields and putting them into a vector.


// Edit: A bunch of other insights, made before I understood the question.

I can possibly imagine an algorithm working like this:

vector<2dCoord> result = data.filter(DataType::Green);
for (2dCoord in result) {
    // do some operations on data[2dCoord]
}

The implementation of filter in a simple unoptimized way would be to scan the whole array and push_back matching fields to the vector.

If you shall need more complicated queries, lazily-evaluated proxy objects can work miracles:

data.filter(DataType::Green)
    .filter_having_neighbours(DataType::Red)
    .closest(/*first*/ 100, /*from*/ 2dCoord(x,y))
    .apply([](DataField& field) {
        // processing here
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top