Unit4
Unit4
Polygons
P O LYG O N R E P R E S E N TAT I O N A N D A R E A F I L L I N G A L G O R I TH MS
Polygon
•Polygon is a word derived from two Greek words Poly and Gon. Poly means multi and gon means
angle. The meaning of polygon is multi-angle figure.
•“Polygon is a closed figured with many vertices and edge(line segment), and at each vertex exactly
two edges meet and no edge crosses the other”. The simplest possible polygon is a three-sided
triangle.
•
The polygon are called regular polygon as the total number of angles is equal to the total number of edges. Polygon
are largely divided into two classes:
1) Convex Polygon
2) Concave Polygon
1) Convex Polygon –
A convex polygon is a polygon in which if you take any two points of polygon then all the points on the line
segment joining these two points fall within the polygon itself.
Convex Polygon
2) Concave Polygon –
If the line joining any two points of a polygon does not fall entirely within the polygon, then that polygon is
called concave polygon.
Concave Polygon
Many Display devices treat the entire polygon as a single unit. For such devices, the polygons are preserved and
stored as one unit. On the other hand, there are device which cannot treat a complete polygon as one unit.
Such devices draw polygon using successive scan – line. The letter primitive is called Trapezoid Primitives.
This trapezoid primitive uses vector generator algorithm where two successive scan – line are drawn and in this
way a complete polygon is drawn.
Trapezoid
COMPLETE UNIT
3.1.Polygon Inside Tests
Polygon generated so far are left hollow. If a polygon is required to be shown as a solid object, all
the pixels inside the polygon are required to be turned on.
We need to test which points fall inside the polygon and which do not. There are mainly two
methods used to test whether a points is inside or outside the given polygon.
There are such a two method to find out the whether the point is inside or outside the polygon.
Suppose a point,(X0,Y0), is in test and it is required to check whether it falls within a given
polygon of nine sides. Nine vertices are denoted by a, b, c, d, e, f, g, h, I .
d
b
c . f
g
g
.
d
c
b f
The total number of intersection on each side, of the point in test is, one on left, and three on right. That is on both
the sides the total number of intersection is odd.
The test says that if on both the sides the total number of intersection occurred is odd, then the point in test is within
the polygon.
If any one side, the number of intersection is even in number, the point in question is out side the polygon.
d
S9 g
S6 S5
b c
.q
f .r L3
S1 e S4
L2
.p i
S8
S7 L1
S2 S3
a h
In above diagram p, q, and r such where we need to apply some extra tricks to the basics arguments.
For the point p and r, the scan – lines associated with them i.e. L1 and L3, pass through the vertices I and f
respectively, of the polygon. As these two points are vertices, they are the end points of two edges of the sides of
polygon.
For both the edges S2 and S3, one of the end vertices is point through which the scan – line passes. And also the
other end point of both of the edges S2 and S3 are on the same side of the scan – line L1, that is, on the lower side
of the scan – line.
When the intersection point is a vertex of the polygon and the other two end points of the participating edges are on
the same side of scan – line, the intersection is considered to be even.
The left of the point p, there is one intersection and to its right there are even plus odd that is odd number of
intersection. Thus the point p is now inside the polygon.
For point r, our scan – line passes through vertex of the polygon. This f is a point, which is shared by edges S4 and
S5.
The other two end point of both S4 and S5 are on different sides of the scan – line. The intersection at the point f is
considered to be odd.
To the left of the point in test r, there are three intersection and also to its right, there is one intersection, so now
point r is also inside the polygon.
When a scan – line passes through a vertex of the polygon, arbitrarily any one edge is considered ending above the
scan – line. Among the line S4 and S5 , S4 crosses the scan – line; in this case, edge S5 does not cross the scan –
line.
When the scan – line passes through a vertex of those two edges whose other end point are on the opposite sides
generate odd number of intersection.
In case of vertex I, which is shared by the edges S2 and S3, other end point of both of these two edges are on one
side of the polygon.
Both of the edges cross the scan – line and that is why both the edges generated intersection point.so, it considered
as even.
One f the edges of the polygon is a part of the scan – line that passes through point q. This happened because edge
S1 is horizontal and it is a part of the scan – line.
Here edge S1 which happens to be a part of scan – line passes through two vertices b and c of the polygon.
According to the rational presented above, any one of S6 and S7 can be considered to cross scan – line. Edge S7
will generate an intersection point with the scan – line.
No intersection point will be generated by S6 and similarly no intersection point will be generated by the edge S1.
Total intersection will be odd in number to the left of the point p and also to the right of this point. The point p is
inside the polygon.
When the polygon is not simple, that is when a polygon is complex or overloaded the even – odd method does not
work efficiency.
.
Even – Odd method
2) Winding Number Method
Winding number method is yet another method to check whether a given point is inside or outside
the given polygon.
d g
b
c
. f
. i
Rubber Band
Moving Across the
boundary of
polygon
a h
In this alternative, directions are given to all the edges of the polygon.
g
c f
b
e
1 i
-1
1
h
a
Example :-
The edges which are emanating from the lower half of the scan – line and going through the upper half are
given the direction value 1. otherwise, the direction value is -1.
After assigning the direction value to all the edges, add them. If the total sum of direction values is non – zero, then
the point in question is considered to be the inside the polygon, otherwise the point in test is considered to be outside
the polygon.
Example :-
in this edge hi is originating from the lower half of the scan – line and across the scan – line and the same is
the case with the edge ‘ab’. So, these two edges are given the direction values 1.
Against this edge ‘ia’ is starting from the upper part and goes down the scan – line so this edge is given value -1. The
total sum of the direction values, in this case is 1 – 1 + 1 = 1, which is non – zero and that is how the point in test is
considered to be inside the polygon. d
g
c f
b
e
1 i
-1
1
.
h
a
There are a few other methods that can also be used for testing whether a given point belongs to the polygon or
not.
If the polygon is convex, then we can test whether the point is inside or outside by checking the side of the
point relative to the edges of the polygon.
This method works on the fact that the end point of one edges of the polygon is the start point of the other
edge and also the start point and the end point of the polygon are the same. This way we can create a path for
a polygon.
.
(2,4) (6,4)
(3,3)
.
(1,1) (2,1)
Polygon with an inside and outside point
Let us assume a point (x,y) which is a point in test and the polygon points are (X0, Y0) and (Xi, Yi) where ⅈ = 𝟏 𝟏 𝒏
Now compute the sides index Si and ⅈ = 𝟏 𝟏 𝒏 for the point in test for two successive end point (X0,Y0) and (Xi,Yi) of
an edge of the polygon using the given formula.
𝒔𝒊 = 𝒚 − 𝒚𝟎 𝒙𝟏 − 𝒙𝟎 − 𝒙 − 𝒙𝟎 𝒚𝟏 − 𝒚𝟎
If Si is less than 0, then the point is considered to be on the right side of the edges. If Si is greater than 0, then the
point is considered to be on the lift and if Si is equal to 0, then the point in question is on the edges.
If all values of Si are on the same side, the point in question is considered to be inside. Otherwise, the point is outside
the polygon.
𝒙, 𝒚 = 𝟑, 𝟑
𝒙𝟎 , 𝒚𝟎 = 𝟐, 𝟏
𝒙𝟏 , 𝒚𝟏 = 𝟔, 𝟒
𝒔𝒊 = 𝒚 − 𝒚𝟎 𝒙𝟏 − 𝒙𝟎 − 𝒙 − 𝒙𝟎 𝒚𝟏 − 𝒚𝟎
𝒙, 𝒚 = 𝟑, 𝟑
𝒙𝟎 , 𝒚𝟎 = 𝟐, 𝟏
𝒙𝟏 , 𝒚𝟏 = 𝟔, 𝟒
=(3–4)(6–2)–(3–2)(4–4)
= ( -1 ) ( 4 ) - ( 1 ) ( 0 )
=(-4) -(0)
= -4
POLYGON AREA FILLING
In real world application, almost all the objects are solid objects.to make the figure look
like those of solid objects, it is required to be filled in with some color or pattern.
Polygon are filling means changing the color of pixels belonging to the polygon. This can
be done by different methods.
The following are two methods, which is used popularly for polygon:
Flood fill method is also known as seed fill method. In this method, a point inside the polygon is needed.
This point is called a seed point and the process of filling the polygon area is started from this point.
From the seed point, a point is taken and interior colour is replaced by the required colour. This process is
done till a pixel of boundary colour reached.
This process is performed simultaneously in all the directions. This can be done in two ways:
In four connected points neighbourhood method, four surrounded pixels are taken simultaneously and
replace with the required colour.
𝑥, 𝑦 + 1
−𝑥 𝑥
𝑥 + 1, 𝑦
𝑥 − 1, 𝑦
y−1, 𝑥
−y
Four connecting point flood-fill function
Void four_flood (int pseedx, int pseedy, int pfillcol, int ppolycol)
{
if ( ( getpixel (pseedx,pseedy) != ppolycol ) && ( getpixel( pseedx,pseedy) != pfillcol) )
{
putpixel (pseedx,pseedy,pfillcol);
for_flood (pseedx-1,pseedy,pfillcol,ppolycol);
for_flood (pseedx+1,pseedy,pfillcol,ppolycol);
for_flood (pseedx,pseedy-1,pfillcol,ppolycol);
for_flood (pseedx,pseedy+1,pfillcol,ppolycol);
delay(1000);
}
}
2) Eight connected points neighbourhood method
In eight connected points neighbourhood method, eight surrounded pixels of the seed point are considered
simultaneously.
Recursive function is the best-suited concept for the flood fill algorithm.
𝑥, 𝑦 + 1
𝑦
𝑥 − 1, 𝑦 + 1 𝑥 + 1, 𝑦 + 1
−𝑥 𝑥
𝑥 − 1, 𝑦 𝑥 + 1, 𝑦
𝑥 + 1, 𝑦 − 1
−y
𝑥 − 1, 𝑦 − 1
y−1, 𝑥
EIGHT connecting point flood-fill function
{
putpixel ( pseedx,pseedy,pfillcol);
eight_flood(pseedx-1,pseedy,pfillcol,ppolycol);
eight_flood(pseedx+1,pseedy,pfillcol,ppolycol);
eight_flood(pseedx1,pseedy-1,pfillcol,ppolycol);
eight_flood(pseedx,pseedy+1,pfillcol,ppolycol);
eight_flood(pseedx+1,pseedy+1,pfillcol,ppolycol);
eight_flood(pseedx-1,pseedy-1,pfillcol,ppolycol);
eight_flood(pseedx-1,pseedy+1,pfillcol,ppolycol);
eight_flood(pseedx+1,pseedy-1,pfillcol,ppolycol);
delay(1000);
}
}
Disadvantages:-
The important limitation of flood fill algorithm is, this algorithm does not work with the overlapping and self –
intersecting polygons.
This algorithm will treat such the polygon as a collection of different polygons.
Overlapped
Self Intersection
If we do not have a seed point to fill he polygon, we can turn all the pixels “ on ” which are inside the polygon to fill
the polygon. The problem here is, this needs inside test to be performed with each and every pixels on the screen.
SCAN – LINE fill method :-
The scan – line fill method is a very efficient and cheaper alternative. This is the method in which scan – lines are
used for the filling process. Scan – line fill method works very efficiently with self – intersecting polygons as well.
Checking for each and every pixels on the screen is very time – consuming and costly too. But we can restrict this
process to the smallest rectangle which fits the polygon. That means the rectangle created from the lower left
coordinates of polygon to upper right coordinate of the polygon.
If we consider the largest y value and go through the smallest value of y by scanning left to right, for each value of y
horizontal straight lines will be generated. Each of this horizontal lines is called a scan – line. As our algorithm is using
scan – line, it is called scan – line fill algorithm.
In this alternative, directions are given to all the edges of the polyAssume that we have a boundary of a blank polygon in
our empty frame buffer, which is bounded by smallest possible rectangle around it. All pixels in the scan – line by scan –
line and when we encountered a pixels with dthe intensity of the boundary pixels, we enter into the polygon.
gon.
g
c f
b
e
h
a
Finding intersection points is very easy if the line equation of two lines are known.
d
g
b c f
e
a
h
Pixel between two boundary pixels
There is no need to test each and every edge of the polygon for the intersection with each scan – line. It is sufficient to
test only those edges of the polygon for which y value of scan – line lies between maximum and the minimum y values
of the polygon side.
This can be made easier by logically arranging all the edges of the polygon in the order of their values of y. every time we
steps down to new scan – line, we have to check which are the edges being considered and also examine which are
edges to be dropped from the consideration.
Simply be examine the lowest and the highest y values of the top edges in the indexed edges list with the y values of the
scan – line. All the edges for which y values of current scan – line lies between y values of its end point, are under
consideration.
In order to reduce the efforts, keep indices for start edge and end edge in the sorted edges list. This is possible as we
have all the edges in the sorted form. This way we need to consider only those edges which lies between these two
indices.
When a new edge is added to the consideration, the start index will be shifted bye one vis a vis for the end index. Once
we get the intersection points on polygon edges with the horizontal scan – line, our task is to turn on all the pixels on
the scan – line which are inside the polygon.
Now line segment will be drawn between the intersection points 1 and 2, and 3, and 4 whereas no line will be drawn
from the odd number of intersection point, point 2 to point 3.
Now draw line segment between the two successive pair of x value.
1
2
4
3
1) Determine the smallest possible rectangle, which fits in the polygon to be filled.
3) Determine all the polygon edges which are required to be considered with the current scan – line.
4) Find the intersection points between the scan – line and all the edges under consideration.
7) Shift to the next scan – line, and also check for the possible exclusion and inclusion of new boundary of the
polygon for the consideration.
BOUNDARY FILL :-
Boundary fill algorithm is another polygon or area filling algorithm. The problem with the scan – line fill algorithm is that
for every scan – line, intersection points between the scan – line and the polygon sides are to be founded, which is
very much complex and time – consuming as well.
This boundary fill algorithm uses scan – lines to fill polygons but it does not require cumbersome calculation of finding
intersection points.
This algorithm only needs an insides point to start the process, color of the boundary and a stack.
Let ( x, y ) be an inside point. Starting from this point, the algorithm tests neighboring pixel on the same scan – line to
determine whether it is of boundary color or not. If it is having boundary color, the algorithm will stop in that direction
for that scan – line.
If the color of that pixels is not same as needed, then that pixel will be colored by the desired color and then the next
pixel will be checked. This process will be repeated for all the pixels till the boundary colored pixels is reached.
This process will be performed on all the scan – line of the polygon. To remember the position of pixels, a stack may be
used. The arrow show the direction of painting the color of pixels.
When the process begins, the start position will be pushed on to the stack, that is row six R6 will be pushed. When it
reaches the right boundary, R7 will be pushed on to the stack.
When painting of row seven will end at the left boundary, R7 will be popped out from the stack and R8 will be pushed on
to the stack.
When this process is completed for the upper part of the image, the stack position is same as the one. Now again row
seven R7 will be painted but now in the opposite direction, and this will complete the painting in the lower part of the
polygon.
BOUNDARY FILL METHOD
R13
R9
R8
R1 R7
R6
BOUNDARY FILL
Filling Polygon With Pattern
Many times polygons are needed to be filled-in with a pattern instead of colours. Filling polygon with a given pattern is
a very easy process with raster display. The methods of filling a polygon with a pattern is also known as pattern fill.
Pattern fill is nothing but tiling a predefine pattern repetitively, side by side all over the interior part of the polygon.
In flood fill algorithm, we change the colour of all the pixels which are inside the polygon. We have to do the same thing
but every time an interior pixels will be checked with an appropriate pattern pixels and accordingly it will be coloured.
Do this process for all the pixel of pattern matrix and for all the pixels of the given polygon.
0 0 0 1 1 0 0 0
0 1 0 1 0
0 0 0 1 1 0 0 0
1 0 1 0 1
0 0 1 1 1 1 0 0
0 1 0 1 0
1 1 1 1 1 1 1 1
1 0 1 0 1
1 1 1 1 1 1 1 1
0 1 0 1 0
0 0 1 1 1 1 0 0
0 0 0 1 1 0 0 0
0 0 1 1 0 0 0 0
1 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1
0 0 0 0 1 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 1 0 0 0
1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0
1 1 1 1 0 0 0 0
1 1 1 1 0 0 0 0
1 1 1 1 0 0 0 0
1 1 1 1 0 0 0 0
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1
Various pattern and their
0 0 0 0 1 1 1 1
frame buffer
0 0 0 0 1 1 1 1