Algorithm series (12) Polygon Region filling algorithm--scanning line seed filling algorithm

Source: Internet
Author: User
Tags range

1.3 Scanning line seed filling algorithm

The advantages of the two seed filling algorithms described in sections 1.1 and 1.2 are very simple, with the disadvantage of using recursive algorithms, which not only require a lot of stack space to store adjacent points, but are inefficient. In order to reduce the recursive call in the algorithm and save the use of stack space, many improved algorithms are proposed, one of which is the scanning line seed filling algorithm. The scanning line seed filling algorithm no longer uses recursion to deal with "4-unicom" and "8-unicom" adjacent points, but by filling the pixel segments along the horizontal scan line to deal with "4-unicom" and "8-unicom" adjacent points. In this way, it is necessary to press the starting point of each horizontal pixel segment into a special stack instead of the recursive algorithm, which can save stack space by pressing all adjacent points that have not been processed around the current position. It should be said that the scan line filling algorithm is only a way to avoid recursion, improve efficiency of the idea, the previous mentioned injection filling algorithm and boundary filling algorithm can be improved to scan line filling algorithm, the following is a combination of the boundary filling algorithm of the scan line seed filling algorithm.

The basic process for scanning line seed filling algorithms is as follows: When given a seed point (x, Y), first fill a section in the given area on the scan line of the seed point in two directions to the left and right, and note the scope of the section [Xleft, Xright], and then identify the upper, The next two scan lines are located in the section within a given area and are saved sequentially. Repeat this process until the fill is finished.

The scan line seed filling algorithm can be implemented by the following four steps:

(1) Initializing an empty stack for storing seed points, and putting the seed points (x, y) into the stack;

(2) to determine whether the stack is empty, if the stack is null to end the algorithm, otherwise take out the top of the stack as the current scan line of the seed point (x, y), Y is the current scan line;

(3) from the seed point (x, Y), along the current scan line to the left and right two directions, until the boundary. The left and right endpoint coordinates of the marked section are Xleft and xright respectively;

(4) Check the y-1 and y + 12 scan lines adjacent to the current scan line respectively in the interval [xleft, pixels in xright], which are searched from the xleft to the xright direction, and if there are non boundary and unfilled pixel points, find the rightmost one of these adjacent pixels. and pressing it into the stack as a seed point and then returning to step (2);

The most critical part of this algorithm is step (4), which is to look for new seed points from the previous scan line and the next scan line of the current scan line. One of the more difficult to understand here is why just check the pixels in the new scan line [Xleft, Xright]? What if the actual range of the new scan line is larger (and less continuous) than this range? I checked a lot of computer graphics books and papers, as if there is no special explanation for this, which makes many people in the course of learning this has lingering doubts. The spirit of "destroying" the tireless thinking, this article on the wordy explanation, hoping to lift everyone's doubts.

If the actual point on the new scan line is larger than the [Xleft, Xright] interval of the current scan line, and is continuous, the first (3) step of the algorithm deals with this situation. As shown in figure (4):

Fig. (4) The increase and continuity of the new scan line interval

Assuming that the currently processed scan line is line 7th of the yellow dot, you can get an interval [6,10] after the 3rd step. And then step 4th, search right from the 6th column of the adjacent lines 6th and 8th two scan lines, and determine that the red two dots are the seed points of the 6th and 8th rows, and then in order (6, 10) and (8, 10) Two seed points into the stack. The next loop will process (8, 10 This seed point, according to the 3rd step of the algorithm, will be filled from (8, 10) to the left and right, because there is no boundary point in the middle, so the fill will be until the boundary is encountered, so even though the actual area of the 8th row is larger than the 7th line [6,10] But it still gets the right padding.

If the actual point on the new scan line is larger than the [Xleft, Xright] interval of the current scan line, and there is a boundary point in the middle, how does the algorithm deal with it? Although there is no explicit method to deal with this situation in the algorithm description, the 4th step is to determine the method of the seed point of the upper and lower adjacent scan lines, and the principle of the right pick point, which actually implies the method of bypassing the barrier points from the adjacent scan line. The following illustration (5) illustrates:

Fig. (5) The increase and discontinuity of the new scan line interval

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.