Computer graphics (scan line filling algorithm)

Preface

Just record it briefly

text

To put it simply, the principle is to scan the graph from top to bottom and color the line segments within the graph. The difficulty here mainly lies in how to determine whether the line segment is inside the graph or outside the graph.

Let’s take a closer look at the picture above. The answer lies in the picture. For a scan line that does not pass through a vertex, such as y=6, there is always an even number of intersections with the polygon, and segments located inside the polygon alternate with segments located outside the polygon. For scan lines that pass through vertices, such as y=1, y=2, and y=7, the intersection points with the polygon may be either an even number or an odd number. But if we divide it further, some of these scan lines passing through the vertices pass through extreme vertices, such as y=1 and y=7, and the number of their intersections is an odd number; while some pass through non-extreme vertices, such as y=2 , the number of their intersection points is an even number. In this case, it is better to do a special process and treat all extreme vertices as two points to ensure that the intersection between the scan line and the polygon is always an even number.

Of course, it makes sense to make the number of intersections into an even number. After that, you can pair these intersections from left to right, and all the pixels between the two successfully matched points will be colored. For example, the sequence of intersections between the scan line y=6 and the polygon in the picture above is ABCD, and the two pairs from left to right are AB and CD, and then color the spaces between AB and color the spaces between CD.

So in simple terms, the steps of this method are:

  1. Intersection: Calculate the intersection point of the scan line with each side of the polygon.
  2. Sorting: Sort all intersection points in increasing order of x value.
  3. Pairing: the first with the second, the third with the fourth, etc.; each pair of intersection points represents an intersection interval of the scan line and the polygon.
  4. Color filling: Set the pixels in the intersecting interval to the polygon color.

To illustrate with a specific example, assume that a scan line intersects the boundary line of a polygon at four points A, B, C, and D. These four points divide the scan line into five intervals [0, 2], [2, 3.5], [3.5,7], [7,11], [11,2]. Among them, the two intervals [2,3.5] and [7,11] fall within the polygon, and the pixels in this interval should take the polygon color. Pixels in other intervals take the background color.

It should be noted that the scan line polygon area filling algorithm has the advantages of being intuitive, geometrically meaningful, and taking up less memory space, but it cannot be directly used for surface shading.

Conclusion

There seems to be nothing more to say, so that’s it

Supongo que te gusta

Origin blog.csdn.net/m0_73872315/article/details/134151529
Recomendado
Clasificación