I generate a polygon inside a map. Every point corresponds to an integer value. My current filling algorithm works for most polygons, but as soon as I get convex polygons it has problems :
Both problems are pretty obvious when looking at the picture, sometimes it fills the exterior the only reason that this doesn't happen all the time is because I check to see if the next fill is even(It's always supposed to be even since fills are done by region, ie 0 then 2 then 4.)
The other problem is that since I draw pairs of points, since I treat consecutive rows of 1's as one point the next segment is ignored since there's only one point left.
Here's the filling code :
void PolygonGenerator::fillInside(std::vector< std::vector<int> > &vec, std::vector<CoordinateXY> &vertices)
{
for(unsigned int i = 0; i < vec.size(); i++)
{
std::vector<int> pointCoords = countOnALine(vec[i], i, coordinates);
if(pointCoords.size() <= 1) continue;
for(unsigned int j = 0; j < pointCoords.size(); j+=2)
{
int pt1 = pointCoords[j];
int pt2 = pointCoords[j+1];
while(pt1 <= pt2)
{
if(j % 2 == 0)
vec[i][pt1] = 1;
pt1++;
}
}
}
}
std::vector<int> PolygonGenerator::countOnALine(const std::vector<int> &vec, int lineIndex, std::vector<CoordinateXY> &vertices)
{
std::vector<int> pointCoords;
for(unsigned int i = 0; i < vec.size(); i++)
{
if( (i+1) >= vec.size()) continue;
if(vec[i] == 1 && vec[i+1] == 0)
pointCoords.push_back(i);
}
return pointCoords;
}
I hope the code is self-comprehensible. I don't use the vertices in the countOnALine method or the lineIndex. The method's purpose is simply to return non-consecutive coordinates of 1's.
I've spent too much time trying to figure this thing out, I'm done for today. I hope someone has a clue. I know of another method to fill polygon's, but it is pretty shitty.