Let's talk about how to use the Scanline algorithm to implement polygon fills today.
First of all, what is the scan line algorithm: the input of the algorithm is a set of polygon vertex coordinates (x, y), note that there is a clockwise or anti-clockwise problem, the reader himself, please test; the output is a filled polygon. Pixel fills between XLO and Xhi by using a scan line along the x-axis to determine the boundaries of the polygon on each scan line. Scan lines y=y_i, such as from the y_min of polygons to Y_max, thus covering the entire polygon.
Please refer to the blog "area fill algorithm and polygon fill scan line algorithm", author Twinklingstar. This article uses its source code to populate a pentagram.
First, the coordinates of the pentagram are given below:
Double D_polygon[10][2] = {{13.0901,7.7548},{11.9097,4.1221},{14.9999,6.3672},{18.09,4.1221},{16.9097,7.7548}, { 20,10},{16.1802,10},{15,13.6326},{13.8196,10},{10,10}};
Since the input of the program must be an integer, x, y is enlarged by 10 times times and rounded.
int round_polygon[10][2]; int factor = 10; for (int i = 0; i <, i++) {for (int j = 0; J < 2; J + +) {D_polygon[i][j] *= factor; ROUND_POLYGON[I][J] = round (d_polygon[i][j]); } }
The program defines a vector structure, so the array value is assigned to the struct body.
Vector POLYGON_FIVE_STAR[10]; for (int i = 0; i < i++) {polygon_five_star[i].x = round_polygon[i][0]; POLYGON_FIVE_STAR[I].Y = round_polygon[i][1]; }
Now, call the scan line function Scanlinefill ().
Scanlinefill (Polygon_five_star,10,rgb (0,255,0));
The parameters are: Poylon_five_start, the polygon structure to be filled, 10, the number of vertices of the polygon, RGB (0, 255, 0), fill color. The output is shown below.
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7E/D0/wKioL1cJqDrCnHVYAAATQFV5fq8763.png "title=" five_ Star.png "alt=" Wkiol1cjqdrcnhvyaaatqfv5fq8763.png "/>
Here are a few notes:
Polygon vertices must be counter-clockwise, and then study why not clockwise;
Vertex coordinates need to be integers;
The program also calls the Glut Toolkit, please refer to the previous blog post about the use of glut.
This article from "Hu A Knife" blog, declined reprint!
Scan line algorithm fills pentagram