0% found this document useful (0 votes)
50 views16 pages

CG Unit 4 Polygon

Uploaded by

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

CG Unit 4 Polygon

Uploaded by

zaki18602
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 16
POLYGON 45 UNIT: 4 POLYGON 4.1 Polygon Representation 4.2 Polygon Inside Tests 4.3 Polygon Area Filling Algorithm 4.4 Filling Polygon with Patterns Exercise jump2Learn Publication [www.jump2learn.com] 46 POLYGON Unit — 4Polygon Polygons are used incomputer graphics to compose images that are three- dimensional in appearance. Usually (but not always) triangular, polygons arise when an object's surface is modeled, vertices are selected, and the object is rendered in a wire frame model. This is quicker to display than a shaded model; thus the polygons are a stage in computer animation. The polygon count refers to the number of polygons being rendered per frame. 4.1 Polygon Representation Polygon is a representation of the surface. It is primitive which is closed in nature. It is formed using a collection of lines. It is also called as many-sided figure. The lines combined to form polygon are called sides or edges. The lines are obtained by combining two vertices. Example of Polygon 1. Triangle 2. Rectangle 3. Hexagon 4. Pentagon Following figures shows some polygons. Ee ® Fig: 4.1 - Polygons Types of Polygons: Concave Polygon &Convex Polygon. A polygon is called convex of line joining any two interior points of the ~ Polygon lies inside the polygon. A < — non-convex polygon is said to be concave. A concave polygon has one Fig:4.2 - Types of Polygon interior angle greater than 180°. So OLYGON " a7 Fig:4.3 ~ Convex Polygon shapes Fig:4.4 ~ Concave Polygon shapes that it can be clipped into similar polygons. A polygon can be positive or negative oriented. If we visit vertices and vertices visit D c E o E B on ; 2 F A A 4 Polygon with positive orientation Polygon with negative orientation Fig:4.5 - Orientation of Polygon produces counterclockwise circuit, then orientation is said to be positive. 4.2 Polygon Inside Tests This method is also known as counting number method. While filling an object, we ft identify whether particular point is inside the object or outside it. There are se side an object or two methi ‘an identify whether particular point ods by which we can identi outside, The methods are: Even-odd Method (Rule) and Winding number method (Rule) a Jump2Learn Publication (www.jump2learn.com] POLYGON 48 Be 4.2.1 Even odd method (Rule) ir fhe line from any point i i ,dge crossing along tl In this technique, we will count the edge rarer ai ions i e point (x, (xy) to infinity. If the number of interactions is odd, then the p = oleate Point; and if the number of interactions is even, then the point (x,y] a The following example depicts this concept. Te NZ\A4 | Fig:4.6-Even Odd Method 1 rece. From the above figure, we can see that from the point (x,y), the number of interactions point on Fig. 4.0 -Test Casez the left side is 5 and on the right side is 3. From both ends, the number of interaction points is odd, so the point is considered within the object. There are certain cases when this particular method works with some special implementations which are shown below. Case: 1 Figure 4.7 demonstrates the problem that results when a vertex of the polygon falls directly on the intersection points if the two edges. Since sides a,b both touch the intersection points, should they both generate a node?N, total no of intersection will be 2 on each side of the test it was outside of the polygon. lo, because then there would be Point and so the test would say b The solution to this situation is simple.Points which are intersecting here will be checked according to the edge residing. Here the a edges are on opposite sides of each other. So the total no of intersection which has be i in blue color will be Considered as odd whi (red) and the point is said to be interior. Jump2Learn Publicati n [Www.jump2learn.com) POLYGON Cast considered as an interior point. Case: 3 Figure 4.9 shows the case of a polygon in which one of its sides lies entirely on the scan line passing from the point test. Simply follow the rule as described concerning Figure 4.9:.Side ¢ intersects in on point, because it has one endpoint below the scan line and its other endpoint on-or-above the scan line.Side d does not intersect, because it has both endpoints on-or-above the scan line.And side e also does not intersect, because it has both Fig. 4.9 - Test Case3 endpoints on-or-above the scan line. 4.2.2. Winding number method (Rule) is This method is also used with the simple polygons to test the given poi interior or not. It can be simply understood with the help of a pin and a rubber band. Fix up the pin on one of the edge of the polygon and tie-up the rubber band in it and then stretch the rubber band along the edges of the polygon. jump2Learn Publication [www.Jump2learn.com] 49 POLYGON, When all the edges of the polygon are covered by the rubber band, check out fe pin which has been fixed up at the point to be test. If we find at least one wind at the point consider it within the polygon, else we can say that the point is not inside the polygon. Fig. 4.10 - Winding number method In another alternative method, give directions to all the edges of the polygon. Draw a scan line from the point to be test towards the left most of X direction. + Give the value 1 to all the edges which are going to upward direction and all other -1 as direction values. + Check the edge direction values from which the scan line is passing and sum up them. + If the total sum of this direction value is non-zero, then this point to be tested is an interior point, otherwise it is an exterior point. + In the above figure, we sum up the direction values from which the scan line is passing then the total is 1-1 + 1 = 1; which is non-zero. So the point is said to be an interior point. 4.2.2 Some Other methods (ay 4) 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. There are two facts to be considered © G4) (2) Fig. 4.11 - Alternative method Jump2Learn Publication [www.jump2learn.com] POLYGON ' testis point (x, ‘0. of edges in t Yo) and (x,y) ah “¥) and polygon points are (xo,yo) and (x,y), he polygon). We have to find the side index ) with the help of following equation; Anis the total ni Sifor the successive points (xo, Si= (y= yo) (41 = 0) (x ~ xo) ty = yo) point (3,3) is -3, -4 and-5. So we can say that the point (3,3) is an interior point. There is one more method to test the point is inside the polygon or not. It can be tested by finding the sum of all the angles created with the end points of all the edges of the polygons. If the sum of all the angles @ is 27, then the point is said to be the interior Fig. 4.12 - angles created with the end point, otherwise exterior point. In the Fi8-4-1%-anslescreat fig.4.12 (a) the sum of the angles created is 2m so it is an interior point. This method is the simplest one as well as effective with the self-intersecting polygons too. 4.3 Polygon Area Filling Algorithm i in the following figure. For filling is list of vertices as shown in tl Polygon is an ordered ou need to determine the pixels falling on the border of polygons with particular colors, ¥' me polygon. in this chapter, we will see Now we can the polygon and those which fall inside t fill polygons using different techniques. tlon [www.Jump2learn.com] Jump2Learn Publica 51 POLYGor 52 M a 4.3.1 Flood Fill Algorithm The flood fill method is also known as Seed Fill Method. We need a point inside the polygon here for starting the filling. This point is called the seed point. From the point is taken and the default color is being replaced by the fill color. The seed point the " untered. This process is same process is repeated till the boundary color pixel is enco performed in all the directions. There are basically two ways to fill the polygon: Four connected point neighborhood and eight connected point neighborhood methods. In four connected point method color of four surrounded points of the seed point will be changed with the fill color are left-right and upper-lower of the seed point. Whereas in eight connected point neighborhood method all surrounded points of the seed point will be replaced with the fill color (see fig. 4.13). There is a limitation of the flood fill algorithm; it does not work with the overlapping and self-intersecting polygons. Such polygons are treated as a collection of different polygons. In case if there is no seed point defined then the fill can be done by turning all the pixels on within the boundary of the polygon. The difficulty in this manner is first we have to test the point is inside the polygon or not, if it is inside the polygon then turn the pixel on and change the default color with the fill color. It seems to be a very costly alternative. The algorithm can be written as follows: Flood Fill Algorithm [Floodfill (seedx, seedy, fcol, dcol)] (2) Four Connected neighborhood points fb) Fight Connected neighborhood points \eighborhood p Fig:4.13 - Flood Fill Methods a POLYGON 53 (@connected neighborhood point) 1: Initial Step-1: Initialize the value of seed point (Seeds, seedy), feolor and deo! Step-2: Define the boundary values of the Polygon. on, Step-3: Check if the current seed till the boundary Pixels rea If getpixel(x,y) col then repeat step 4 and 5 Step-4: Change the default color with the fil Color at the seed point. setPixel(seedx, seedy, fcol) Step-5: Recursively follow the procedure with four neighborhood points. FloodFill (seedx 1, seedy, fcol, deol) FloodFill (seedx + 1, seedy, fcol, deol) FloodFill (seedx, seedy - 1, fcol, deol) FloodFill (seedx — 1, seedy + 1, fcol, deol) Step-6: Exit {8 connected neighborhood point) Step-1: Initialize the value of seed point (seedx, seedy), fcolor and deol. 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) = deol then repeat step 4 and 5 Step-4: Change the default color with the fill color at the seed point. ep-4: setPixel(seedx, seedy, fcol) P- i ei d points. tep-5: Recursively follow the procedure with four n ighborhood p Step-5: Recursiv FloodFill (seedx - 1, seedy, fol, do!) Jump2tearn Public: POLYGON 54 SO Floodfill (seedx + 1, seedy, fcol, dco!) Floodfill (seeds, seedy - 1, feo deo!) Floodfill (seedx, seedy + 1, feol, dco!) Floodill (eedx —1, seedy + 1, feol, dco!) FloodFill seedx +1, seedy + 1, feol, dco!) FloodFill (seedx + 1, seedy - 2, feol, dco!) Floodfll (seedx — 1, seedy - 1, feo, deol) Step-6: Exit equirems The flood fill method can be used with any one algorithm as Per the req jent it i hi of the user. The 8 connected neighborhood point method is more fast then the other one. 4.3.2. Scan line Fill Algorithm The scan-line fill method is very efficient and cheaper alternative.It is also known as alternative fill method. In this method scan lines are used for filling the polygons. It is working effectively with the self-intersecting polygons. In this we have to use the smallest rectangle which fits the polygon. Which needs the rectangle to be created from the lower left corner to the upper right corner coordinates of the polygon. For generating the scan lines we have to consider the largest Y value and move towards the smallest Y value a nd draw a horiz, i ‘ontal line among all possible values of ¥. Since we are using scan-lines to fill the polygon th On thi: i known as scan-line fill method. ‘gon this method is POLYGON 55 values of the edges endpoint which are stores d in the frame buffer. We have to consider only those lines where the edge of polygon for which y values of scan-line lies between maximum and minimum values of y values of the polygon side.For that we have to arrange all the edges of the polygon in order of their values of (b) ing from scan-line by scan-line we have to check which edges are ee eer a dges to be dropped from the consideration. This can be ro Se ee ee for different set of y indexes in the frame buffer, Fig. 4.16 shows the edges wI jump2Learn Publication [ww jum 56 POLYGON ss a problem when the scan-line is passing from the polygon and certain portion Now there eee caee of the scan-line is not inside the polygon where as other portion is inside the polygon (see figure 4.16). For solving this we have to draw a line between two successive intersections starting with the odd number to the next successive point. 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 or the polygon. Then draw a line segment between two successive pairs of the x values (see figure 4.17), Fig. 4.17 - scan-lines with more than 2 intersections POLYGON 5? Step-2: Arrange al the edges of polygon - 1 Orde ler of y val Step-3: Determine all the Polygon ves current scan-line. edges BES which are required to be considered with also check for the possible exclusion and inclusion of new boundary of the Polygon for the consideration. Another approach to this algorithm is findi y app ing the pixels of boundary color on the scan-line, which is nothing but the intersection Points of the scan-lines and the polygon edges. It will work when only one polygon of the given colors there on the screen, 4.4 — Filling Polygon with pattern Sometime it is required to fill the polygon with certain patterns instead of colors. In such cases other polygon filling method cannot be used. As we are using the flood fill method to change the color of the interior points of the polygon, in similar way we have to "a 0 11110000 1000000 COSMET ett Me EEEES EMAL ETE foro oon11109 13329955 © Sdboto00 91010 0131199 tiiiooo0 o0001000 110d T2i11t2 Qoggtii? 99001900 O99 gtiisao SBSbi tit © 1ggggags 00011009 oebo1111 10000000 9,0.0.2.2 8 insand their frame buffers 1 Fig. 41 p- Patter! POLYGON 58 Il be colored. ropriate pattern pixel and accordingly it wil ee atrices as shown in the attern mi For generating such patterns, we are using the pi figure below. tangle which fits According to this algorithm we have to first construct oom ree an et pas the polygon in it. Then take the first pixel from the pattern ™ a, of the rectangle and if the pixel of rectangle is inside the polyé ee pattern matrix to the first pixel of the polygon. We have to repeal pixels which are interior to the polygon (see figure 4.19). (2) The Filling Process (©) The Pattern to be used Fig:4.19 ~ Pattern filling Method Pattern Fill Algorithm Step-1: Determine the smallest possible rectangle which fits the polygon in it. Step-2: Generate the pattern matrix to be used for the Polygon filling. Step-3: Check the first Pixel of rectangle wi then move to step-4 otherwise move to step-5. of the polygon. Jump2Learn Publication [www.Jump2learn.com] POLYGON a Step-5: repeat the procedure from step-3 until the complete polygon filled with the pattern. Step-6: Exit 59 POLYGON 60 —— ers ‘Answer in Short en odd method to perform inside test on polygon. State the limitations of evé f boundary fill algorithm. Write any one advantage and disadvantage o Explain convex and concave polygon in short. State advantages of winding number method over even-odd method. Define concave and convex polygon. 2 Fev Differentiate between convex and concave polygon. Answer in Detail 1. List the methods available to check whether the given point is inside the polygon Or not. Explain any one method in details. 2. Explain boundary fill method to fill polygon. 3. Compare scan line and flood fill algorithms for polygon. Define slope. Discuss various types of slope by giving proper example. List the polygon filling methods. Explain any one method in details. Discuss the inside test method that work efficiently with over Lapping polygons. 6. Write a note on boundary fill algorithm. 7. What is slope of line? Calculate slope of line having two points P1 (6,3) and P2(-2,3) on the line. 8. Explain even-odd method to perform inside test on polygon. 9. Discuss process of animation. 10. Explain scan line fill method to fill a polygon. 11. Explain winding number method to perform inside test on polygon

You might also like