0% found this document useful (0 votes)
2 views14 pages

Unit 4

The document provides an overview of polygons in computer graphics, detailing their definitions, classifications (regular, irregular, convex, and concave), and methods for determining if points are inside or outside polygons using the Even-odd and Winding Number methods. It also discusses polygon area filling algorithms, including Flood Fill, Scan Line Fill, and Boundary Fill methods, highlighting their processes and limitations. The content is aimed at students of computer graphics at ROFEL BBA & BCA College, Vapi.

Uploaded by

jainpratham687
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views14 pages

Unit 4

The document provides an overview of polygons in computer graphics, detailing their definitions, classifications (regular, irregular, convex, and concave), and methods for determining if points are inside or outside polygons using the Even-odd and Winding Number methods. It also discusses polygon area filling algorithms, including Flood Fill, Scan Line Fill, and Boundary Fill methods, highlighting their processes and limitations. The content is aimed at students of computer graphics at ROFEL BBA & BCA College, Vapi.

Uploaded by

jainpratham687
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

ROFEL BBA & BCA COLLEGE, VAPI

UNIT – 4
POLYGONS
Course 601 – 1 : Computer Graphics

ASST. PROF. ZINKAL PATEL


ROFEL BBA & BCA COLLEGE, VAPI
Unit – 4 Polygons

4.1 Polygon Representation


 Polygon is a word derive from two Greek words poly and gon. Poly means multi
and gon means angle. So the polygon is multi angle figure.
 Polygon is a closed figure with many vertices and edges and at each vertex exactly
two edges meet and no edges crosses the other.
 The polygons are classified into various types based on the angles and sides. The
types of polygons are listed as follows –
o Regular polygon
o Irregular polygon
o Convex polygon
o Concave polygon
 Regular polygon
 If all the sides and interior angles of a polygon are equal, the polygon is said to be
the regular polygon. Examples of the regular polygon include equilateral triangle,
rhombus, square, and many more. In the below image, you can see examples of
regular polygon.

 Irregular polygon
 If all the sides and interior angles of a polygon are of different measure, the
polygon is said to be the irregular polygon. Examples of the irregular polygon
include rectangle, kite, scalene triangle, and many more. In the below image, you
can see examples of an irregular polygon.

Computer Graphics Asst. Prof. Zinkal Patel Page 1


Unit – 4 Polygons

 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.

 Concave polygon
 A concave 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 does not fall
entirely within the polygon.

Computer Graphics Asst. Prof. Zinkal Patel Page 2


Unit – 4 Polygons

4.2 Polygon Inside Tests


 If a polygon is required to be shown as a solid object, all the pixels inside the
polygon are required to be turned on. For this we need to test which points fall
inside the polygon and which do not. There are mainly two methods used to test
whether a point is inside or outside the given polygon.
 These methods are,
1. Even-odd Method
2. Winding Number Method

4.2.1 Even-odd method


 This method is known as Counting Number Method. Suppose a point say (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,…,i. A polygon is shown in diagram. The
dark dot is the point in test.

 The even-odd method uses the scan-line, which passes through the point in test.
Let us assume that the coordinates of the point are (x0, y0), so the scan-line used
is the horizontal line xi = y0. The scan-line is show in following diagram.

Computer Graphics Asst. Prof. Zinkal Patel Page 3


Unit – 4 Polygons

 There are some intersection between the scan line and the sides of the polygon.
These intersection are denoted by cross marks (x) in the diagram.
 The total number of intersections on each side of the point in test is, one on left
and three on right. That is on both sides the total number of intersections is odd.
 The test says that if on both the sides the total number of intersections occurred is
odd then the point in test is within the polygon.
 If on any one side, the number of intersections is even in number, the point in
question is outside the polygon. There are some situations where the logic
described so far does not work properly.

 In above diagram points p, q and r are such where we need to apply some extra
tricks to the basic argument.
 For the points 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. In this case, if we consider it as one, then it is a problem for point p.
 In this case, to the right of point p, there are two intersections and to its left there
is one. So clearly this point will be considered as outside the polygon. This is not
true.
 And if we consider it as even number of intersections, then point r will create a
problem. In this case, to the left of point r, there will be four intersections, which
are even and to its right there is one intersection and that is odd. So the point r is
outside the polygon which is not true.

Computer Graphics Asst. Prof. Zinkal Patel Page 4


Unit – 4 Polygons

 Now the point i, that is shared by two edges S2 and S3. 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 points 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.
 In such a situation, 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. Now the left of the point p, there is one
intersection and to its right there are even plus odd that is odd number of
intersections. Thus point p is now inside the polygon.
 For point r, our scan-line passes through vertex f of the polygon. This f is a point,
which is shared by edges S4 and S5. The other two end points of both S4 and S5
are on different sides of the scan-line. In this situation, the intersection at point f
is considered to be odd. Now to the left of the point in test r, there are three
intersections and also to its right, there is one intersection, so now point r is also
inside the polygon.
 Now the remaining problem is with the point q. One of the edges of the polygon is
a part of the scan-line that passes through point q. This is because the 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. So here any
one of the S6 and S7 can be considered to cross scan-line.
 Let us assume that edge S7 crosses the scan-line. In this case, edge S7 will
generate an intersection point with the scan-line. No intersection point will be
generate by S6 and similarly no intersection point will be generated by the edge
S1. Thus the total intersections will be odd in number to the left of the point p and
also to the right of this point. This way the point p is inside the polygon.
 Disadvantages :
 This method will not work when the polygon is complex or overlapped.

Computer Graphics Asst. Prof. Zinkal Patel Page 5


Unit – 4 Polygons

4.2.2 Winding number method


 The winding number method is another method to check whether a given point is
inside or outside the given polygon.

 Take a rubber band and tie up a pin at one end of the rubber band and move the
other end of the rubber band along the edges of the polygon. When it finishes a
complete cycle, check out the pin which is located on the point in test. If on the
pin, we find at least one wind, the point is inside the polygon otherwise point is
outside the polygon. But this argument cannot be programmed.

 In this method directions are given to all the edges of the polygon. The directions
of edges are as following.

 Now, a horizontal scan-line from the point in test to the leftmost value of X is
drawn. Then all the edges, which intersect with the scan-line are given directions
value.
 For 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 values to all the edges, add them. If the total sum of
the direction value is non-zero, then the point in question is considered to be
outside the polygon, otherwise the point in test is considered to be outside the
polygon.
 For example as show in following diagram, edge ‘hi’ is originating from the lower
half of the scan-line and crosses the scan-line and the same is the case with the
edge ‘ab’ these two edges are given the direction value 1.

Computer Graphics Asst. Prof. Zinkal Patel Page 6


Unit – 4 Polygons

 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.

4.3 Polygon Area Filling Algorithm


 Polygon area filling means changing the color of pixels belonging to the polygon.
This can be done by following three methods.
1. Flood Fill method
2. Scan line fill method
3. Boundary Fill method
4.3.1 Flood Fill Method

 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. In this method a point is taken and
interior color is replaced by the required color. This process is done till a poxel of
boundary color is reached. This process is performed simultaneously in all the
directions. This can be done in two ways:
1. Four connected points neighbourhood method
2. Eight connected points neighbourhood method.
 Four connected points neighbourhood method : In four connected points
neighbourhood, four surrounded pixels are taken simultaneously and replaced
with the required color.

Computer Graphics Asst. Prof. Zinkal Patel Page 7


Unit – 4 Polygons

 Eight connected points neighbourhood method : In this method, eight


surrounded pixels are taken simultaneously and replaced with the required color.
 For flood fill algorithm recursive function is the best concept to use.
 Limitation : This algorithm does not work with the overlapping and self-
intersecting polygons.

4.3.2 Scan line Fill Method

 The scan line fill method is a very efficient and cheaper alternative. Some book
describe this method as “alternative fill method”. 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 pixel on the screen is very time-consuming and costly
too. so we restrict this process to smallest rectangle which fits the polygon. That
means the rectangle created from the lower left coordinates of polygon to upper
right coordinates 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 these horizontal lines is called a scan-line. As our algorithm is
using scan-line, it is called scan-line fill algorithm.

Computer Graphics Asst. Prof. Zinkal Patel Page 8


Unit – 4 Polygons

 Now, assume that we have a boundary of a blank polygon in our empty frame
buffer, which is bounded by a smallest possible rectangle around it.
 Let us examine all pixel in the box scan line by scan line and when we encounter a
pixel with the intensity of the polygon boundary pixel, we enter into the polygon.
We will remain in the polygon till we encounter a second pixel with the intensity
of polygon boundary. All in between pixels are turned on. As shown in following
diagram.

 The major difficulty with this scheme is with a point where two edges meet each
other. For example, see the vertex i in above diagram. This problem can be solved
by considering line instruction stored in display file instead of considering
boundary points from the frame buffer.
 Finding intersection points is very easy if the line equations of two lines are
known. Here, we know the line equations of polygon edges as well as the
equation of the scan-line.
 Again here, 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 and 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 step
down to new scan line, we have to check which are the edges being considered
and also examine which are the edges to be dropped from the consideration.
 This can be done, simply by examining the lowest and the highest y values of the
top edges in the indexed edges list with the y value of the scan line. All the edges

Computer Graphics Asst. Prof. Zinkal Patel Page 9


Unit – 4 Polygons

for which y value of current scan line lies between y values of its end points, are
under consideration.

 Above diagram shows how edges are considered. First diagram is for the
intersection with a scan line and second diagram, when an edge is dropped from
the consideration. The thicker edges 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 lie between these two indices.
 When a new edge is added to the consideration, the start index will be shifted by
one and that applied for the end index too.
 Once we get the intersection points on polygon edges with the horizontal scan
line, out task is to turn on all the pixels on the scan line which are inside the
polygon.
 Another issue arise when we have more than two intersection points on a single
scan line. For example, in following diagram pixels between the intersection
points 1 and 2, and 3 and 4 belong to the polygon whereas pixels between the
intersection points 2 and 3 are outside the polygon.
 To resolve this problem, we have to draw a line between two successive
intersection points starting from odd number of intersection points 1 & 2, and, 3
& 4 whereas no line will be drawn from the odd number of intersection points, i.e.
point 2 to point 3.

Computer Graphics Asst. Prof. Zinkal Patel Page 10


Unit – 4 Polygons

 The result shown in above diagram. In other words, arrange all intersection points
in the order of their x values, the smallest x value is the left most boundary and
the largest x value will be on the right most boundary of the polygon. Now draw
line segments between the two successive pair of x values.

4.3.3 Boundary Fill Method


 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 found, 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 pints.
 This algorithm only needs an inside 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 test 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 pixel 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 pixel is reached.
 This process will be performed on all the scan lines of the polygon. To remember
the position of pixels, a stack may be used.
 The following figure shows the process of boundary fill.

Computer Graphics Asst. Prof. Zinkal Patel Page 11


Unit – 4 Polygons

 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
complete for the upper part of the image, the stack position is same as shown in
above diagram.
 Now again row R7 will be painted but now in the opposite direction and this will
complete the painting in the lower part of the polygon.

4.4 Filling polygon with patterns


 Many times polygons are needed to be filled in with a pattern instead of colors.
Filling a polygon with a given pattern is a very easy process with raster displays.
 The method of filling a polygon with a pattern is also known as pattern fill. Pattern
fill is nothing but tiling a predefined pattern repetitively side by side all over the
interior part of the polygon.
 In flood fill algorithm, we change the color of all the pixels which are inside the
polygon. Here, we have to do the same thing but every time an interior pixel will
be checked with an appropriate pattern pixel and accordingly it will be colored.
 For this we need, predefined pattern matrices and some of the patterns are
shown as following.

Computer Graphics Asst. Prof. Zinkal Patel Page 12


Unit – 4 Polygons

 According to pattern fill, consider a smallest rectangle within which the polygon
fits perfectly. Now take the first pixel from the pattern matrix, and check the first
pixel of the rectangle and if that pixel is inside the polygon, the copy all the details
of the first pixel from pattern matrix to the first pixel of the polygon. Do this
process for all the pixels of pattern matrix and for all the pixels of the given
polygon.

Computer Graphics Asst. Prof. Zinkal Patel Page 13

You might also like