Can't properly fill my polygon

Started by
3 comments, last by DDoS 11 years, 3 months ago

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 :

fuck_zps1c560e0a.png

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.

Advertisement
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.
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.

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

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

This topic is closed to new replies.

Advertisement