Scanline Triangle Filling (simplified for single triangles)

This technique fills polygons one scan-line at a time. For each scan-line in the window, we must determine which polygons the scan-line intersects.

The scan-line method takes advantage of edge coherence, i.e.

 

  1. Many edges that are intersected by scan-line $y$ are also intersected by scan-line $y+1$.
  2. The x-value of the point of scan-line intersection migrates predictably from scan-line to scan-line. The x co-ordinate of the intersection of an edge with a scan-line increases by $\frac{1}{m}$ for each scan-line, where $m$ is the slope of the edge.

An important component in the data structure needed for this algorithm is the edge node. Each edge in the polygon is represented by an edge node. An edge node contains the following information;

 

  1. $y_{upper}$ , the $y$ co-ordinate of the upper end-point of the edge.
  2. $y_{lower}$ , the $y$ co-ordinate of the lower end-point of the edge.
  3. $x_{int}$ , initially the $x$ co-ordinate of the lower end-point of the edge. As the algorithm progresses, this field will store the $x$ co-ordinate of the point of intersection of the scan-line.
  4. $y_{m}$ , the reciprocal of the slope of the line ( $\frac{1}{m}$ ).

When constructing an edge node for the edges, the following must be taken into account;

  1. Horizontal Edges are ignored (an edge node is not generated for these), these edges will be drawn when the adjacent edges are processed.
  2. If two edges share a vertex AND the vertex is an upper end point for one edge and a lower end point for another edge, the $y_{upper}$ of the lower edge must be reduced by $1$.

 

We also need an Active Edge List (AEL) which contains the two edge nodes with the lowest $y_{lower}$ . The AEL represent the two edges intersected by the current scan line. The AEL needs to be maintained as we move from on scan-line $y$ to the next scan-line $(y+1)$ ;

 

  1. Add any edges whose $y_{lower}$ is equal to $y$ .
  2. Fill the pixels from the $x_{int}$ of one edge to the $x_{int}$ of the next.
  3. One or both of the edges in the AEL will no longer be intersected by $(y+1)$ . Remove the edges from the AEL, where $y_{upper}$ is equal to $y$ .
  4. The intersection point of the scan line with the remaining edges will change for the new scan-line. $x_{int}$ is updated to $x_{int} + y_{m}$ . Note that $x_{int}$ must be stored as a real number to avoid round-off errors.
  5. increment y (to the next scan line)
  6. Repeat until the Edge List and AEL are empty.