• Create Account

# Can't properly fill my polygon

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.

4 replies to this topic

### #1Zilee  Members   -  Reputation: 103

Like
0Likes
Like

Posted 31 December 2012 - 08:06 PM

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.

Edited by Zilee, 31 December 2012 - 08:06 PM.

### #2Álvaro  Crossbones+   -  Reputation: 19700

Like
0Likes
Like

Posted 31 December 2012 - 11:03 PM

The polygons where you are having trouble are not convex: They are concave. Since rasterizing arbitrary polygons is a tricky process, people usually restrict themselves to convex polygons, or even just triangles.

### #3Zilee  Members   -  Reputation: 103

Like
0Likes
Like

Posted 01 January 2013 - 12:50 PM

The polygons where you are having trouble are not convex: They are concave. Since rasterizing arbitrary polygons is a tricky process, people usually restrict themselves to convex polygons, or even just triangles.

Yep sorry, concave. Ah well, I saw some algorithms that "seem" to get it done. The most processing intensive way of doing it is with the parity rule and check if each number is inside or out the polygon, but that is rather intensive on the processor. I've looked into flood filling methods, but they expect you to give them a point inside the polygon, something I can't really do in some polygon generations schemes. Oh and I messed up with the modulo in the first code, it's actually useless if you implement the delimited regions correctly.

Anyways, I'm going to work on it.

### #4eppo  Crossbones+   -  Reputation: 4409

Like
0Likes
Like

Posted 01 January 2013 - 02:24 PM

It might be easier to take a two-step approach: first convert the concave n-gon to triangles, then rasterize the individual triangles.

### #5DDoS  Members   -  Reputation: 312

Like
1Likes
Like

Posted 01 January 2013 - 05:03 PM

use the scanline algorithm, it's alot faster than filling every triangle

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.

PARTNERS