MC0072-Computer Graphics

1.) Describe the theory of Polygon and Pattern filling along with their corresponding algorithms. Ans.)

Polygon Filling

Filling the polygon means highlighting all the pixels which lie inside the polygon with any color other than background color. Polygons are easier to fill since they have linear boundaries. There are 2 basic approaches used to fill the polygon. One way to fill a polygon is to start from a given “seed point” known to be inside the polygon and highlight outward from this point i.e neighboring pixels until we encounter the boundary pixel. This approach is called seed fill because color flows from the seed pixel until reaching the polygon boundary. Another approach to fill the polygon is to apply the inside test i.e to check whether pixel is inside the polygon or outside the polygon and then highlight pixels which lie inside the polygon. This approach is known as scan line algorithm. Seed fill

This seed fill algorithm is further classified as flood-fill algorithm and boundary fill algorithm. Algorithms that fill interior defines regions are called flood-fill algorithms, those that fill boundary-defined regions are called boundary-fill algorithms or edge-fill algorithms. Boundary Fill algorithm

In this method edges of a polygon are drawn. Then starting with some seed (point) , any point inside the polygon we examine the neighboring pixels to check whether the boundary pixel is reached. If boundary pixels are not reached, pixels are highlighted and the process is continued until boundary pixels are reached. Boundary defined regions may be either 4-connected or 8-connected as shown in the fig if a region is 4-connected, then every pixel in the region may be reached by a combination of moves in only 4 directions: left, right, up and down. For an 8-connected region every pixel in the region may be reached by a combination of moves in the 2 horizontal, 2 vertical and 4 diagonal directions [pic]

In some cases, an 8-connected algorithm is more accurate than the 4-connected algorithm. This is illustrated in fig. here a 4-connected algorithm produces the partial fill. [pic]

Fig. : Partial filling resulted using 4-connected algorithm

Procedure: boundary_fill(x,y,f_color,b_color)

{

If(getpixel(x,y)!=b_color && getpixel(x,y)!=f_color)

{

Putpixel(x,y,f_color)

Boundaryfill(x+1, y, f_color, b_color)

Boundaryfill(x, y+1, f_color, b_color)

Boundaryfill(x-1, y, f_color, b_color)

Boundaryfill(x, y-1, f_color, b_color)

}

}

getpixel() gives the color of specified pixel and putpixel() draws the pixel with specified color. Flood Fill algorithm

Sometimes it is required to fill an area that is not defined within a single color boundary. In such cases we can fill areas by replacing a specified interior color instead of searching for a boundary color. This approach is called flood fill algorithm. Here we start with some seed and examine the neighboring pixels. However, here pixels are checked for a specified interior color instead of a boundary color. And they are replaced by new color. Using either a 4-connected or 8-connected approach we can step through pixel positions until all interior points have been filled. The following procedure illustrates the recursive method for filling 8-connected region using flood fill algorithm Procedure: flood_fill(x,y,old_color, new_color)

{

If (getpixel(x,y)=old_color))

{

putpixel(x,y,new_color)

flood_fill(x+1, y, old_color, new_color)

flood_fill(x-1, y, old_color, new_color)

flood_fill(x, y+1, old_color, new_color)

flood_fill(x, y-1, old_color, new_color)

flood_fill(x+1, y+1, old_color, new_color)

flood_fill(x-1, y-1, old_color, new_color)

flood_fill(x+1, y-1, old_color, new_color)

flood_fill(x-1, y+1, old_color, new_color)

}

}

Scan Line algorithm

Recursive algorithm for seed fill methods have got two difficulties. The first difficulty is that if some inside pixels are already...

## Share this Document

Let your classmates know about this document and more at StudyMode.com