What is scanline fill algorithm?

Share

The scanline fill algorithm is designed to determine the interior parts of a polygon in a rendered image. The algorithm scans the image from top to bottom by avoiding the need to perform complex and time-consuming point-in polygon tests. It has significant applications in rendering engines, games, and 3D modeling tools.

How scanline works

  • The scanline fill algorithm works with lines (one at a time).

  • It intersects each line with all polygon edges and determines which intersections are entry or exit points.

  • The spans between entry and exit points are filled in.

  • It then processes the polygon line by line, from the defined minimum to maximum y-values.

  • The edge tableThe table that contain the coordinates of two endpoints. (ET) is sorted by the lowest y value (or highest, if you render from top to bottom) and contains all the edges of a polygon.

  • Each entry in the edge table contains the data such as the lowest yy coordinate of an edge (ymin y_\text{min}), the highest y coordinate (ymax y_\text{max}), the xx coordinate corresponding to the y=yminy = y_\text{min} (x  of  yminx\text \;{of}\; y_\text{min}), and the inverse of the slope (1/m).

  • The active edge tableA data structure that consists of all the intersection points of the edges with the current scanline. (AET) initially contains no edges.

  • As we move from one line to the next, we add edges from the ET to the AET when we reach their y=yminy = y_\text{min} and remove edges from the AET when we reach the y=ymaxy = y_\text{max}.

  • The edges in the AET are then sorted by their current xx value which can be found by using the formula given below.

Scanline algorithm: Main loop

  • Repeat until the ET and AET are empty.

    • Move any edges from the ET to the AET where, y=ymin.y = y_\text{min}.

    • Sort the AET on xx.

    • Fill the desired pixels on the scanline y using pairs of x coordinates from the AET.

    • Increment yy by 1.

    • Remove edges from AET where y=ymax.y = y_\text{max}.

    • Update each edge's xx value in the AET for the next scanline.

    • Repeat the process.

Handling special cases

Case 1:

Intersection is the point where the two edges of the lines meet.

  • In this diagram, p1p_1 is the intersection point of both the edges e1e_1 and e2e_2. So, we can fill the shape pairwise (pop_o, p1p_1) and (p1p_1, p2p_2).

  • If we compute the intersection of the scanline with the edge e1e_1 and e2e_2 separately, we get the intersection point p1p_1 twice.

  • In order to correctly fill in the shape, we need to count the intersection point twice. Keep both of the points p1p_1 in the AET.

Case 1
Case 1

Case 2:

  • If we count p1p_1 twice here, we will end up filling between p1p_1 and p2p_2.

  • In order to fill it up correctly, do not count p1p_1 twice (pop_o, p1p_1, p1p_1, p2p_2, p3p_3).

Case 2
Case 2

Note: A simple solution to handle the cases mentioned above, is to check if the intersection is the yminy_\text{min}or ymaxy_\text{max} of both the edge's endpoint. If it is, we count it twice, otherwise, we count it only once.

Demonstration

No intersections
1 of 13
Copyright ©2024 Educative, Inc. All rights reserved