Algorithm series (12) Polygon Region Fill algorithm: Improved scan line filling algorithm

Source: Internet
Author: User
Tags definition

Improved scanning-line filling algorithm

The principle and implementation of the scan line filling algorithm is simple, but the requirement for storage space is high because it maintains both the active side table (AET) and the New Edge table (NET). Part of the two tables is duplicated, and the "New Edge table" is in many cases a sparse table, if you can improve it, avoid two tables, you can save storage space, while eliminating from the "side table" to generate "new side table" overhead, but also eliminates the use of "new side table" to maintain the "active side table" overhead, Based on this principle, the original scan line algorithm can be improved.

3.1 Redesign the active side table

The improved algorithm still uses the concept of "active edge table", but no longer constructs the independent "activity side table", but directly in "the side table" delimit part interval as "The Activity border area", namely, divides the polygon the edge to divide into two subsets, one is with the scan line has the intersection point the collection, The other is a collection of edges that have no intersection with the scan line. To do this, you only need to sort the "active edge table" by the vertex ymax coordinates of each edge. This sort and the original scan line algorithm in the "Active edge table" maintenance principle is the same, because only the edge of the Ymax coordinate interval with the scan line intersection edge can be "active edge." To avoid repeated scans of the entire "active side table," You need to use a one-pointer and a last pointer to identify "active border areas." The edges before the first pointer are the edges that have been processed, and the edges after the last pointer are the edges that have not been processed. Every time a scan line is processed, the first and last pointer positions are updated, the position of the last pointer is adjusted to include the edge of the ymax larger than the current scan line to the active border, while the first pointer is adjusted to exclude the finished edge from the "active border".

If the last pointer is adjusted based on whether the ymax of the edge is greater than the current scan line, what is the basis for adjusting the one? How can you tell if a side has been processed? The method is to add an dy (Δy) attribute to the definition of Edge (edge), this property is initialized to the length of this side in the Y direction, each processing a scan line, dy to do a reduction, when the dy=0, it shows that the edge has not been with the scan line, can be excluded from the activities of the border between. The complete definition of the "edge" of the improved scan line algorithm is as follows:

7 typedef struct TAGEDGE2   
       
 8 {   
       
 9     double XI;     double dx;   
       
one     int ymax;     int dy;   
       
}edge2;

The meaning of Xi, DX, and ymax in the EDGE2 definition is the same as that of the edge in the original algorithm, except for the one DY attribute.

Each time a scan line is processed, in addition to the "active border" between the primary and last pointers that need to be adjusted, the "active edge" between the primary and last pointers is sorted in the order of Xi from small to large to ensure that the fill algorithm can be filled with the correct line of intersection line segments. Therefore, each adjustment of the "active border between" the beginning and last pointers to the "activities of the border between the" reordering, that is, "between the activities of the border" within the location of the various sides are not fixed, as the scan line changes in the corresponding changes.

Still take the polygon shown in figure (6) as an example, the "Active side table" Status of the scan line 10 o'clock is shown in figure (11-A), while the "active side table" status for processing scan line 8 o'clock is shown in figure (11-b). As can be seen, the order of the edges within the "active border area" has been adjusted when the scan line 8 o'clock is processed, because the newly added p6p1 and p1p2 two edges are smaller than the intersection coordinates of the scan line of the P5P6 and the scan line, and are therefore in front of the P5P6.

Fig. (11) Improved active edge table structure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.