|
Efficient Polygon Fill Algorithm With C Code Sample © 2007 Darel Rex Finley. This complete article, unmodified, may be freely distributed for educational purposes. Before reading this page, you should thoroughly familiarize yourself with the point-in-polygon algorithm. While the point-in-polygon algorithm is useful for determining whether a few points are inside a polygon, it is woefully inefficient for filling a polygon, because it requires checking every side of the polygon for every pixel in the image. To speed things up tremendously, we will check each side of the polygon only once per pixel row. It works like this: Figure 1Figure 1 shows a polygon. We are about to render one row of pixels. All the pixels on that row have the same Y coordinate, which is represented by the red line in the figure. Loop through the polygon and build a list of threshold-crossing nodes, just as in the point-in-polygon algorithm — but instead of comparing them with an X coordinate, store them all in a list. Figure 1 shows the indices (0 through 5) of the nodes. In this example, the polygon starts at the blue corner, and is traced counter-clockwise, which generates a fairly random horizontal order for the nodes. Figure 2Next, sort the node list so that it proceeds from left-to-right, as shown in Figure 2. This takes a little time, but we have to do it only once per pixel row. Figure 3Now, as shown in Figure 3, it is a simple matter to fill all the pixels between each pair of nodes — from node 0 to 1, node 2 to 3, and node 4 to 5. C Code Sample
Send me an e-mail!
|