Chapter 4
Chapter 4
Step 1 − Find out the Ymin and Ymax from the given polygon.
Step 2 – From each edge of the polygon from Ymin to Ymax, all the egdes are intersected by
Scanline. Each of the points of intersection are named as p0, p1, p2, p3.
Step 3 − Sort the intersection point in the increasing order of X coordinate i.e. (p0, p1), (p1, p2),
and (p2, p3).
Step 4 − Fill all those pair of coordinates that are inside polygons and ignore the alternate pairs.
Flood Fill Algorithm
For some of the polygons, the area and boundary are filled by using different colors. A specific
interior color is used in such cases.
Fill color option is used rather than on the object boundary. Fill color replaces the interior color.
The algorithm is said to be complete when there are no left out pixels of the original interior
color.
The pixels are filled by using either Four-connect or Eight-connect method. The adjacent pixels
are considered.
For the complete object, the boundary color is assumed to be the same. Either 4-connected pixels
or 8-connected pixels are used by this algorithm.
4-Connected Polygon
The pixels are used by placing them on four different sides of the current pixel till a different
color boundary is identified in 4-connected polygon.
Algorithm
Step 1 − Initialize the value of seed point (seedx, seedy), fcolor and dcol.
Step 2 − Define the boundary values of the polygon.
Step 3 − Check if the current seed point is of default color, then repeat the steps 4 and 5 till the
boundary pixels reached.
Step 4 − Change the default color with the fill color at the seed point.
Step 5 − Recursively follow the procedure with four neighborhood points.
FloodFill (seedx – 1, seedy, fcol, dcol)
FloodFill (seedx + 1, seedy, fcol, dcol)
FloodFill (seedx, seedy - 1, fcol, dcol)
FloodFill (seedx – 1, seedy + 1, fcol, dcol)
8-Connected Polygon
8-connected pixels are used in this technique by adding the pixels above, below, right and left
side of the current pixels. In addition to this, the pixels are fixed diagonally such that the
complete area of the current pixel is covered. The process is continued till a boundary is found
with different color.
Algorithm
Step 1 − Initialize the value of seed point (seedx, seedy), fcolor and dcol.
Step 2 − Define the boundary values of the polygon.
Step 3 − Check if the current seed point is of default color, then repeat the steps 4 and 5 till the
boundary pixels reached
If getpixel(x,y) = dcol then repeat step 4 and 5
Step 4 − Change the default color with the fill color at the seed point.
setPixel(seedx, seedy, fcol)
Step 5 – With the four neighborhood points, follow the procedure:
(seedx – 1, seedy, fcol, dcol)
FloodFill (seedx + 1, seedy, fcol, dcol)
FloodFill (seedx, seedy - 1, fcol, dcol)
FloodFill (seedx, seedy + 1, fcol, dcol)
FloodFill (seedx – 1, seedy + 1, fcol, dcol)
FloodFill (seedx + 1, seedy + 1, fcol, dcol)
FloodFill (seedx + 1, seedy - 1, fcol, dcol)
FloodFill (seedx – 1, seedy - 1, fcol, dcol)
Step 6 − Exit
The area marked in the figure, which 4-connected pixel technique failed to fill is filled by the 8-
connected technique.
Inside-outside Test
When an object is filled, it is essential to identify whether the specific point is inside the object
or outside the object. This is done by Inside-outside test also known as counting number method.
An object can be identified whether inside or outside by two methods:
Odd-Even Rule
Nonzero winding number rule
Odd-Even Rule
The edge that crosses along the line from the point (x,y) to infinity is counted. In case of odd
interactions, the point (x,y) is an interior point and in case of even interactions, the point (x,y) is
an exterior point. This concept is depicted by the following example:
It is observed from the above point (x,y), that the number of interaction point on the left side is 5
and on the right side is 3. The point is considered within the object as the number of interaction
points is considered as odd.
Alternatively, directions can be given to all the edges of the polygon. A scan line can be drawn
from the point to be tested towards the left most direction of X direction.
To all the edges going to upward direction the value of 1 is given and for other -1 is
assigned as direction values.
The edge direction values are checked from which the scan line is passing and sum up
them.
The point that is to be tested is an interior point, if the total sum of the direction value is
non-zero, or else it is an exterior point.
The direction values from which the scan line is passed is summed up in the figure
above, and then the total would be 1 – 1 + 1 = 1, which is non-zero and hence the
point is an interior point
Review Exercise
1. Define polygon
2. Explain types of polygon.
3. List Coordinates of neighbouring pixels in 8-c
Coordinates (x,y)
4. List Coordinates of neighbouring pixels in 4-connecte
Coordinates (x,y)
5. Explain inside-outside test of polygon.
6. Each edge and each diagonal of hexagon is either colored red, blue or green Let X denote
the number of triangles with vertices in the hexagon's vertices that are colored with single
color_ Calculate EX.
7. WAP to draw hexagon and fill hexagon with pink color using flood fill algorithm with 8
connected method.