What is scanline fill algorithm?
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
(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.edge table The table that contain the coordinates of two endpoints. Each entry in the edge table contains the data such as the lowest
coordinate of an edge ( ), the highest y coordinate ( ), the coordinate corresponding to the ( ), and the inverse of the slope (1/m). The
(AET) initially contains no edges.active edge table A data structure that consists of all the intersection points of the edges with the current scanline. As we move from one line to the next, we add edges from the ET to the AET when we reach their
and remove edges from the AET when we reach the . The edges in the AET are then sorted by their current
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,
Sort the AET on
. Fill the desired pixels on the scanline y using pairs of x coordinates from the AET.
Increment
by 1. Remove edges from AET where
Update each edge's
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,
is the intersection point of both the edges and . So, we can fill the shape pairwise ( , ) and ( , ). If we compute the intersection of the scanline with the edge
and separately, we get the intersection point twice. In order to correctly fill in the shape, we need to count the intersection point twice. Keep both of the points
in the AET.
Case 2:
If we count
twice here, we will end up filling between and . In order to fill it up correctly, do not count
twice ( , , , , ).
Note: A simple solution to handle the cases mentioned above, is to check if the intersection is the
or of both the edge's endpoint. If it is, we count it twice, otherwise, we count it only once.
Demonstration
Free Resources