c++iteratorpolygonedge-listscanline

Scan Line Polygon Fill Algorithm


I am implementing a scan line based polygon fill algorithm. I know the general algorithm and I am now trying to implement it in C++.

I need to implement the edge table an active edge list.

I have made the active edge list a vector for easy inserting and removing dynamically.

The edge table however is a bit more confusing.

I am trying to implement the edge table using an array of vectors, and the vectors will contain a struct that I made.

Here is the struct.

struct Bucket
{
    // Fields of a bucket list
    int ymax, x, dx, dy, sum;
};

I have a for loop which iterates over an array of vertices passed in, and then it creates a bucket and inserts the bucket into the edge table at the ymin index for an edge.

My problem is that I am having a hard time looping over the edge table and accessing the individual buckets.

Here is the declaration of the array of vectors of buckets:

// This array is the edge table which has a max index of 300
    // The window for the program is never more than 300 by 300
    vector<Bucket> et[300];

Here is my for loop that's iterating over the edge table trying to print out items. I tried using an iterator and a normal int for the index, but neither works when trying to print out each bucket's values.

// Debugging the edge table, prints out all buckets
    vector<Bucket>::iterator it;
    for(int j = 0; j < 300; j++)
    {
            for(it = et[j].begin(); it < et[j].end(); it++)
            {
//                printf(*it);
//                printf();
            }
            for(int q = 0; q < et[j].size(); q++)
            {
                printf("ymax = %d", q[0]);
            }
    }

Solution

  • Thanks to playing around and help from Michael I was able to print out the values. This will now also help me to access these values when I am ready to iterate over the edge table and fill in the polygon.

    The correct way to print the values of the struct inside the vector was to use this for loop:

    vector<Bucket>::iterator it;
        for(int j = 0; j < 300; j++)
        {
            for(int q = 0; q < et[j].size(); q++)
            {
                printf("ymax = %d, x = %d, dx = %d, dy = %d, sum = %d,\n", et[j][q].ymax, et[j][q].x, et[j][q].dx, et[j][q].dy, et[j][q].sum);
            }
        }
    

    I hope this can help others who are also rusty at c++.