0% found this document useful (0 votes)
126 views31 pages

Image To PDF 20240124 23.04.14

Uploaded by

rajmaisuria111
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)
126 views31 pages

Image To PDF 20240124 23.04.14

Uploaded by

rajmaisuria111
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/ 31
POLYGONS computer graphics such as lines, curves and circles. But real-life applications, in addition to lines, curves and circles, need surfaces, planes, shapes, colours, shades, patterns, textures, etc. They add reality to an image and picture. For example, if you want to draw just an clementary figure like a house, you need a triangle and a square as illustrated in Figure 4.1. A { To add solidarity, these images need to be filled with colours and/or patterns. We will deal with colours, shades, ete., in later chapters, but in this chapter, we shall discuss how to create these types of figures. These types of figures are called polygons. S: far we discussed how to generate some basic elements of FIGURE 4.1 House. 41 POLYGON \olygon is a word derived from two Greek words Poly and Gon) Poly means multi apd gon means angle. Thus, the meaning of polygon is multi- angle figure. (“Polygon is a closed figure with many vertices and edges Uline segments), and.gi each vertex exactly two edges meet and no edge Gesses the other”) The simplest possible polygon is a three-sided triangle. ‘ures 4.2(a), (b), (e) and (d) show some of the polygons. : ' J 1 YU E oe an @ oy © @ FIGURE 4.2 Polygons with three, four, five and six sides. 105 106 Computer Graphics The polygons illustrated in Figure 4.2 are called regular polygons as the total number of angles is equal to the total number of edges. Polygons are largely divided into two classes: (i) Convex Polygons, and (ii) Concave Polygons.(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 ff the line joining any two points of a polygon do ll entirely within the polygon, then that polygon is called concave polygon. Figure 4.3 shows convex and concave polygons, Convex polygon Concave polygon. FIGURE 4.3 Classes of polygons. When such polygons are created, it is important to preserve them in our graphics systems. 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 devices which treat a complete polygon as one unit. Such devices draw polygons using successive scan- lines. The latter primitive is called Trapezoid Primitive. This trapezoid primitive uses vector generator algorithm where two successive scan-lines are drawn and in this way a complete polygon is drawn as illustrated in Figures 4.4(a) and (b). & (a) Complete unit Ley Trapezoid FIGURE 4.4 Polygon representation. When a polygon is created, the next question arises is how to preserve @ polygon in a display file. We know that we need one instruction to draw a line through a display file. This instruction draws a line segment between the current position and the given coordinates. But we cannot preserve a polygon by just a single instruction. To preserve a polygon, a many instructions are needed as the number of sides of the polygon. In addition to this, one more instruction is added which tells the starting pom, awe ee 107 of the polygon and also tells about the total number of sides of the polygon. For example, the following table shows a display file which structions for an image like a house. int jven fos ss in ; igure 4.5, opcode is a code of operation to be performed, for example, we grawing a line, 2 for moving the pen without drawing, line, ete. op- : “y are the coordinates of vertices of polygon. X, and oP —Opeote OX CO 5 50 50 1 100 100 1 100 200 1 1 200 1 1 100 1 50 50 FIGURE 4.5 Display file structure for a house like image. Figure 4.6 illustrates how polygons can be used to create images. FIGURE 4.6 Image created using different polygons. bad how, we have studied the concept of drawing a polygon. The following Togram 4.1 shows how a polygon can be drawn on a graphics device. PROGRAM 4.1 Program for drawing a polygon [ee Program for drawing a Polygon ***/ 4 “nclude “Ypeder struct int no _of side; 108 float x1 [10]? float yl [10] float wi [10] 7 }polygons main() { Ly polygon read_pe void draw_polygon (polyg' int gd=DETECT, m7 int pcols polygon polyli ons : 2 on poly ,int x) ebrser 0s gon tds = 0 H po eecinat is the colour of the polygon ?")i scant ("$d", «pcol)] # initgraph (égd, égm," draw_polygon(polyl,pcol) getch(); I /*** Function to Read a Polygon ***/ polygon read_polygon() ( int i7 polygon polyl; float t1,t2; printé ("Enter Numebr of Edges of polygon : scanf ("8d", spolyl.no_of_side); for (i=1;i<-poly1.no_of_side;it+) { printf ("Enter value of &d vertex :",i); scan ("$f $£", 6t1, &t2) ; polyl.x1[i]=tl; polyl.yl(i]=t2; polyl.w1{i]=1; } return (polyl) ; } /*** Function to Draw a Polygon +##/ eo draw_polygon (polygon poly1, int col) int i,noside; noside=poly1.no_of_side; setcolor (col); for (i=1;i<-noside-1; iss) line (polyl.x1[i], poly1.y1 [4 Line (polyl.x1 {noside] realy ytinceeatt -x1 [141], polyl.yil } rpolyl.x1[1],polyl-y SSS ee el PL ell pavers, 109 42 POLYGON INSIDE TESTS @: polygons generated so far are left hollow [ha polyson is rex chown as 28 Tred to be olid object, all the pixels inside the polygon are required ts pe turned On For this, we need (o test which points fall re Phere are ma do not. There are mainly two methods used to est whether a the polygon and which 1 tis inside or outside the given polygon, These methods are (i) Even ig Method (i) Winding Number Method. The following sect Mose two methods with other methods for performing the inside tests ss explain 42.1 Even-Odd Method ¢ oun | ny Number ove ! This method is also known as Counting Number Method. Suppose a point, Say (os Sel) yo), is in test and it is required to check whether it falls within Sipven polygon of nine sides. Nine vertices are denoted by a by 6... is A povgon is shown in Figure 4.7. The dark dot is the point in test h FIGURE 4.7 The point in test. 7 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 (xo, yo), 80 the can-line used is the horizontal line x, = yo. The scan-line is shown in Figure 4.8, a h FIGURE 4,8 Intersections between the sean-line and polYBo": Computer Graphice 110 Pee n find some intersections between the As you can see in Figure Poa Sf the polygon. These intersections = = yo and i (X) in the figure. The total number of z oint in test is, one on left, and three intersections on a ek aa the total number of intersections ig on right.That is aa that if on both the sides the total number of odd The test ieee igadd, then the point in test is within the polygon. intersections occ the number of intersections is gyen in number, the If on any tion is qutside the polygon. ‘There are some situations where the logic described so far does not work properly. Figure 4.9 shows one such a situation. scan-line denoted by cross_mar FIGURE 4.9 Various points in test. = We need some techniques to deal with such situations. In Figure 49, 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 sean-lines associated with them, ie. 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, how many intersection points should be considered? Either one for each of the line segments participating in the intersection or only one intersection point? 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. eye be considered as outside the polygon. This is not es Scan a : as even number of intersections, then point f ane this case, to the left of point 1, there will be four > are even, and to its right there is one intersection, and that is odd. So as per the earlier argument, the point r is now outside the polygon. This is also not true. bserve the point i, that is shared by two edges $2 and $3. For both the edges S2 and 83, one of the end vertices is point through which the sca” line passes. And also the other end points of both of the edges S2 and $3 are on the same side of the sean-line L1, that is, on the lower side of th? scan-line. In such a situation, when the intersection point is a vertex of Polygons cee 11 the polygon and the other two end on the same side of scan-line, the i Now to the left of the point p, there are even plus odd that is Points of the participating edges are intersection is considered to be even there is one intersection and to its right : odd number of intersections. Thus, point is now inside the polygon.JFor point x, our scan-line passes think vertex f of the polygon. This f is" point, which is shared by edges $4 and $5. The other two end points of both $4 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. The main logic behind the rationale presented above is as follows. When a scan-line passes through a vertex of the polygon, arbitrarily any one edge is considered ending above the scan-line. For example, among the lines S4 and $5, S4 crosses the scan-line; obviously in this case, edge S5 does not cross the scan-line. This is how when the Scan-line passéd through a vertex of those two edges whose other end points are on thé ° opposite sides generate odd number of intersections. In case of vertex i, which is shared by the edges S2 and S3, other end points of both of these two edges are on one side of the polygon. According to the presented argument, both of the edges cross the scan-line and that is why both the edges generate intersection points. That is why in such situations, oe are considered even) jow 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 happened because edge S1 is horizontal and it is a part of the scan-line. In this case, how many intersections are to be considered? Here edge S1 which happens to be a part of scan-line passes through two vertices b and © of the polygon. According to the rationale presented above, any one of 86 and S7 can be considered to cross scan-line. Let us assume that edge 87 crosses the scan-line. In this case, 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. 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 polygorf) This method will also work with polygons with holes. For example, refer to Figure 4.10. FIGURE 4.10 Even-odd method for a hollow polygon. 112 Aysadvumtene [The important point to note here is that when the polygon is not simple, That is when a polygon is complex or overlapped the even-odd method does not work efficiently. The following Program 4.2 shows the implementation of the logic of the even-odd algorithm. PROGRAM 4.2 Program for performing the polygon inside test by even-odd method on inside te: 7*** Program to perform polyg #include include #include "c: \tc\include\mygraph.h" main() ( int gd=DETECT, gm; float minx=9999, miny=9999,maxx=0,maxy=0, slope [10], i scan_b,scan_m; float x_section(10],y_section[10],pt_%,Pt_y: int i,j,k, polycol,no_section, j1, 42, count: polygon poly1; clrser(); polyl = read_polygon(); print£("\nGive Colour Number to Draw Polygon :"); scanf ("$d", épolycol); printf ("Give the Point to be Tested\n"); scanf ("Sf %£", ept_x, épt_y); getch(); fe /*** working Area, Slope and Intercept calculation ***/ for (i=1; i maxx) __Maxx=polyl.x1 [i]; if (polyl.y1 [i] > maxy) __maxy=polyl. yt i}; oy ‘polyl.no of side) slope[i]=( (polyl “yl [4] 4 » ae (poly1 yl (4]-polyt.y1 [1}) / (polyl.x1 [i]-polyl-¥1[2]))7 slope(i]=((poly1. yl [itl] : . ~polyl. inter (i] =(polyl.y1[i] ise (i) / (polyL.x1 (i+1]-polyl.#2 ~ (slope [i] *polyl.x1[i]) J: scan_b=pt Fee = ote le area a eee 1a no_sectior for (J=1; 3<=polyl.no_of_side; 34+) i : jisdi i€(j!=polyl.no_of_side} j2ajtls olyl.yl(51]) && (pt_y <= polyl.y11J21) 11 olyl.y1[j1]) && (pt_y >= polyl.y1[52])) no_section++; x_Section[no_section]=abs ( (pt_y-inter{3])/ slope{j]- scan_m)); y_section[no_section]=abs( ( (slope{j]*scan_b) - (inter [J] *scan_m))/ (slope [j]-scan_m))7 , else continue; } print£("No. of Intersection = d\n" getch()7 for (i=1;i<=no_section;i++) { a printf ("Intersection is x[8d y(%d]=8£\n",i,x_section[i],i,y_section[i])+ no_section) ; getch(}; te (i=1;4pt_y) && (pol. yl-yl [32] #include tc\include\mygraph.h" main () int gd=DETECT, gm; float pt_x,pt_y,x_section[10],y_section[10]; int i,},polycol,no_section, j1,}2,count=-9, weight [10] polygon poly1; elrscr(); polyl = read_polygon(); printf ("\nGive Colour i Hye Scant ("$d", spolycol) ; oe oes Print£ ("Give the Poin scanf ("$f ge" it to be Tested\n") ; 1&pt_x, spt_y); for (j=1;}0; i=i-1) ( if (getpixel (i, pt_y) == polycol) { no_section++; x_section[no_section]=(float) i; y_section(no_section]=pt_y; polyl-no_of_side; j++) if (((y_section[no_section]>=polyl.y1{J1]) && (y_section[no_section] <= polyl.y1[j21)) && ((x_section[no_section] >= polyl.x1[j1]) «6 (x_section[(no_section] <= polyl.x1{321))) { weight [no_section]=1; break; } if (((y_section{no_section]<=poly1.yl[j1]) && (y_section [no_section] >= polyl.y1(521)) && ((x_section[no_section] <=polyl.x1[J1]) && (x_section[no_section] <= polyl.x1(j2]))) { weight [no_section]= break: } } ) else continue; ) count=0; for(J=1;j #include #include "c: \tc\include\mygraph.h" main() { int gd=DETECT, gm, i, 31,32, ncount=0, pcount=0, polycol; float pt_x,pt_y, Sideindex [10]; polygon poly17 printf ("Give the Point to be Tested\n scanf ("Sf $£", &pt_x, &pt_y); printf ("Gine Colour Number to Draw Polygon : scanf ("&d", spolycol) getch(); for (i=1;i<=polyl.no_of_side;it+) if( 42-1; else j2ait1 sideindex [i]=((pt_y-poly1-y1(51])* (polyl.x1 (321 ~polyl x1 (311) )~ ((pt_x-polyl.x1 (J1]) * (poly1.y1(J2]-poly1.y1(311))s printf ("Side index = $£\n", sideindex(i]); if (sideindex[i]<0) ncount++; else peount++; } if ( (abs (ncount)==poly1 .no_of_side) | | (abs (pcount) ==poly1.no_of_side)) printf ("Point is Inside"); = else h print£ ("point is Outside geteh(); initgraph(égd, &gm,"") 7 draw_polygon (poly1, polycol) ; ' #include a Hclde OF te include RsGOP main () ( nt gd = DETECT, 9% eloat x1 {10}, y1 (20)? polygon polyl? seedx, seedy, 1, fillcol, polycol; void four_flood(int sx, int sy, int fe, int pc): eleser ()i polyl-read_polygon() 7 4 peinté ("\n Give Polygon Boundary Colour : de scant ("d", spolycol) 7 /++* Read a seed point for flood-fill ***/ printf ("Enter seed Point ")i scanf ("ed 8d", éseedx, &seedy) } printf ("Give colour number to fill polygon scanf ("8d", &£i11col) ; initgraph (gd, &gm,"") 7 draw_polygon(poly1, polycol) 7 /*** Calling Four Connected Flood-Fill Function ***/ four_flood(seedx, seedy, fillcol, polycol) ; delay (1000); eee getch(); closegraph () ; } #88 Fo ‘i ur Connecting Point Flood-Fill Function ***/ void four_flood(int { Pseedx, int pseedy, int pfillcol, int ppolyce!) if ( i (setpixel (Pseedx, pseady) != ppolycol) && i (getpixel (pseedx, pseedy) != pfillcol)) putpixel (pseedx four_flood(pseednn qo PEtL1eol) 5 Four flood (psecays1 Poee¥/PEAl1col, ppolycol) + four flood sPseedy, pfill 7 floor (Pseedx, pseedy-1 eiiesiei eas, x «ppolyco. four_flood (pse. ~ edx, delay (1000) ; Pseedy+1,pfillcol,ppolycol) / Cees a ooo Polygons 123 For eight-way connected point method the four _flood() funetion of Program 4.5 will be replaced by the eight £lood() function PROGRAM 4.6 Program for filling a polygon by using eight-way connecting points method 7=** program for filling a polygon by using eight-way connecting points method ***/ void eight flood (int pseedx, int pseedy, int pfillcol, int ppolycol) "ce ( (getpixel (pseedx, pseedy) |= ppolycol) && (getpixel (pseedx, pseedy) != pfillcol)) { putpixel (pseedx, pseedy, pfillcol); eight flood (pseedx-1, pseedy, pfillcol, ppolycol) eight _flood(pseedx+1, pseedy, pfillcol, ppolycol) + eight flood (pseedx, pseedy-1, pfillcol, ppolycol) eight flood (pseedx, pseedyt1, pfillcol, ppolycol) ¢ eight flood (pseedxt1, pseedy+1, pfillcol, ppolycol) eight _flood (pseedx-1, pseedy-11, pfillcol, ppolycol] eight _flood (pseedx-1, pseedy+1, pfillcol, ppolycol) ; eight flood (pseedx+1, pseedy-1,pfillcol, ppolycol) ; delay (1000); ) b esrnctive Fi method 4.3.2 Scan-line Fill Method / The scan-line fill method is a very efficient and cheaper alternative. Some books describe this method as “alternative fill method”. This is the method in which scan-lines are used for the filling pr jcan-line fill method works very efficiently with self-intersecting polygons as well. ———— Checking for each and every pixel on the screen is very 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 coordinates 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 these horizontal lines is called a scan-line. As our algorithm is using scan-line, it is called scan-line fill algorithm. In general, in computer graphics any algorithm that makes use of scan-lines is classified as scan-line algorithm. 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 as shown in Figure 4.18. Now, the argument is very simple. Let us examine a h FIGURE 4.18 Blank polygon with scan-lines, ne by scan-line and when we encounter a pixe| 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 Pith the intensity of polygon boundary. All in-between pixels are turned gn, as shown in Figure 4.19. The major difficulty with this scheme is with a point where two edges meet each other. For example, see the vertex i in Figure 4.19. This problem can be solved by considering line instructions 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. In our case, we know the line equations of polygon edges as well as the equation of the scan-line. all pixels in the box scan-li x h FIG i: ‘URE 4.19 Pixels between two boundary pixels. Again here, there i for the ae no need to test each and every edge of the polygo with ae edges of the polygon for ot Scam-line. It is sufficient to test only thos® Polygons 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 jndexed edges list with the y value of the scan-line. All the edges for which y value of current scan-line lies between y values of its end points, are ‘under consideration. Figure 4.20 shows how edges are considered Figure 4.20(a) for the intersection with a scan-line and Figure 4.20(b) 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. (@) ) FIGURE 4.20 Edges with respect to scan-lines. When a new edge is added to the consideration, the start index will be shifted by 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. Drawing line between intersection points using our standard line drawing algorithm or any other equivalent algorithm can do this. This is also illustrated in Figure 4.22. But the problem arises when we have more than two intersection points on a single scan-line. For example, in Figure 4.21, 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 FIGURE 4.21. More than two intersection points on one scan-line. 126 outside the polygon. Pomputer Graphicg fe this problem, we have to draw a ling n points starting from odd number of a line segment will be drawn between, To resolve | jive intersectio! intersection points. 3 and 4 whereas no line will be di i 42, and, irawn the intersection from the odd number points youndary an the polygon. ints 1 an nanears A : ons of intersection points, 1.e- point 2 to point 3. The ds, arrange alll intersect mimaure 4.22. In other words, all intersection ; ie thelr “values; the smallest x value is the left-mogt in the ote largest x value will be on the right-most boundary of d the oar line segments between the two successive par of x values. 1 FIGURE 4.22 Line between two intersection points. Here it is very important to note that when our scan-line passes through a point, which is shared by two edges of the polygon, the intersections are determined by the even-odd method. Let us list the steps of scan-line fill method in order to summarize it. 1. 5. 6. wt ‘The scan-line fill al; . Determine the smallest possible rectangle, which fits in the polygon to be filled. . Arrange all the edges of polygon in the order of y values. Determine all the polygon edges which are required to be considered with the current scan-line. Find the intersection points between the scan-line and all the edges under consideration. - : ee Arrange all intersection points in the order of their = val “aw a line segment between two successive pairs of intersection points. (Gada te cogmamp edd to even) “e : i iié]]and also check for the possible shift to the next, scan-lit exclusion and inclusion of mew boundary of the polygon fer tH consideration, mine fill elgorithm is implemented in the following Progsam 47 Tien Tote eset minty | We Is meach oor ap do bottom 719 mex) Of the. Poly gan proGRAM 4.7 Program for filling a polygon by sc an-line program for filling a polygon by sca fill methoa ne fill method wae ginclude pinclude yinelude "c: \te\include\mygraph.h" main() t {nt gd=DETECT, gm; inx=9999, miny=: a Satin eeantas ¥°9999,maxx-0,maxy-0, slope(10], inter [10], float x_section[10],y_section{10]; int i,j,k, £illcol, polycol,no_section, j1, 42; polygon polyls yoid sort_array (float [], float [], int); elrser ()f polyl = read_polygon(); print£ ("\nGive Colour Number to Draw Polygon oe scané ("8d", spolycol) printf ("\nGive Colour Number to Fill Polygon :"); scant ("8d", &£illcol) ; /+** Working Area, Slope and Intercept Calculation ***/ for (i=) ; ic=polyl.no_of_side; i++) if(polyl.x1[i] maxx) maxx=polyl.*1 [4] # if (polyl.yl [i] > maxy) polyl .no_of_side) i i (polyl.x1(i]- slope [i]=((polyl-yilil polyl-y1 {1} / (Ps yay x2 (24 else 7 yl.x1(4+1)— slope[i]=((polyl-y1li+t] (poly polyl.x1(i])) tay*polyt xi (41) -polyl-y1{il)/ inter(i] =(polyt.y? [il- (slope initgraph (agd, &gm,"") draw_polygon (poly1, polycol) # as ints ***/ /*** calculations of Intersection Points Computer Graphicg 128 scan no_section=0; for (j=1;5<=P° fl ly no_of_side:3+*) jel? if (((4>polyl-yl [j1)) && (1 polyl-y1lj2]))) ‘ no_section++; : ; : x section[no_section]=abs ((i-inter(j]) /(slope[j]~- f a scan_m)); y_section{no_section]=abs ( ( (slope(j]*scan_b) ~ (inter [4] *scan_m))/(slope[j]-scan_m)); } else continue; sort_array(x_section,y section,no_section); for (K=1;k<=no_section;k=k+2) { if ((no_section == 0) || (no_section == 1)) continue; draw_dda (x section [k],y_section{k],x_section{k+1],y_section{k+1] ,fillcol) ; getch ()7 } ) t getch(); closegraph ({) ; + /*** Punction for Sorting an Array ***/ vit sort_array(float arrx[],float arry[], int n) int i,j, tempx, tempy; for (i=lyiarrx[5+1)) { tempx-arrx(4+1]; tempy=arry(j+1]; arrx(j+1]=arrx (4); arry(j+l]=arry[3]; Polygons tote eee nee 129 arrx[j]=tempx; arry(j]=tempy; bisad vantage (Another approach to implement this algorithm is finding the pixels of boundary colour on a scan-line. These pixels are none but the intersection points between a scan-line and the polygon boundary. But this approach will work efficiently only if only one polygon of the given colour is there on the screén} This logic is implemented in the following Program 4.8 PROGRAM 4.8 Program for alternative scan-line fill method /*** Program for Alternative Scanline Fill Method =**/ #include #include #include "c:\tc\include\mygraph.h" main() { int gd=DETECT, gm; polygon polyl; float minx=9999, miny=9999, maxx=0,maxy=0,scan_b; float x_section[10],y_section[10]; int i,j,k, fillcol, polycol,no_section, j1, 42; void sort_array(float [],float [], int); clrser(); polyl = read_polygon()i print ("\nGive Colour Number to Draw Polygon :"); scanf ("8d", spolycol) 7 printf ("\nGive Colour Number to Fill Polygon :")i scanf("$d", 6fillcol); /*** Working Area Calculation ***/ for (i=l; i<=polyl.no_of_side; i++) { if(polyl.x1[i] maxx) maxx=polyt.x1[4]# if (polyl.yl [i] > maxy) maxy=polyl-y1 (ili Compute 130 cee eee ee = MPR Srepicn printé ("Working area is (3f» $£) and (sf, #f)",minx,miny,maxx,maxy) getch()F ae initgraph (sod, som, "") 7 ; draw_polygon (polyl, polycon) + for (isminytl;icmaxysit*) ( no_section=07 ; for (j=minx; j<=maxxi j++) ‘ olycol) if (getpixel (3,4) ( no_sectiont+; x_Section{no_section] y_section{no_section] : i sort_array(x_section, y_section,no_section); for (k=1;k<=no_sect ion; k=k+2) { if ((no_section continue; 1)) ) II (no_sectio draw_dda(x_section(k],y_section[k],x_section(k+1],y section[k#1], fillcol) ; delay (10000); ) ) ) geteh() + closegraph() ; ) /*** Pundtion to Sort an Array ***/ void sort_array(float arrx(],float arry[{], int n) ( int i,j,tempx, tempy; for (i=l; i<=n;itt) { for (j=1)4<=1 { if(arrx(j)>arrx(4+1)) { -initt) tempx=arrx([j+1]; tempy=arry(j+1}; arrx(j+1)=arrx(3); arry(j+1)marry(j]; arrx(j]=tempx; arry(j]=tempy; Here it is noticeable that Program 4.8 is not working properly as th even-odd inside test method is not incorporated in the logic. However, iL would be interesting for the reader to modify this program to make it run efficiently. 43.3 Boundary Fill © Boundary fill algorithm is another polygon or area filling algorithm. The problem (with the scan-line fill algorithm is, that Tor -line, amin points etneen the scan e and the polygon sides are to bi found//which is very much complex and time-consuming as well. This boundary fill algorithm uses scan-lines to fill polygons but i does not require cumbersome calculation of finding intersection points. This algorithm only needs an inside point to start the process, colour of the Boundary and a stack, (x, y) be an inside point. Starting from this — point, the algorithm neighbouring pixel on the same scan-line to determine whether it is of boundary colour or not. If it is having boundary colour, the algorithm will stop in that direction for that scan-line. If the colour of that pixel is not same as needed, then that pixel will be coloured by the desired colour and then the next pixel will be checked. This process will be repeated for all the pixels till the boundary coloured 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. The arrows show the direction of painting the colour 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 RB 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 shown in Figure 4.23. 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. The logic for boundary fill is implemented in the following Program 49. dverd , disadvern ise cme ahruce points CHO? 3 —> Thic meted (UST ramp auellh eae he belle Hey J Bounds y colt enteric , IF A) eye poorer My, epgoryy iy eet lig dl fori thera cfoet nor 4hem 4 aple => TH shod work only afith 7 SY : a oets with corpler 4d farges PatTIeN, Computer Graphics 132 ci R8 R7 R6 FIGURE 4.23 Boundary fill. PROGRAM 4.9 Program for filling a polygon with a given pattem using boundary fill algorithm /*** program for filling a polygon with a given pattern using boundary fill algorithm ***/ #include #include #include "c:\tc\include\mygraph.h" main() { int gd=DETECT, gm; int i,j,k, fillcol, polycol, seedx, seedy, fil_flag=1,dir_flagl=1; int minx=9999, miny=9999, maxx=0,maxy=0, nseedx, nseedy; polygon poly1; elrser(); polyl = read_polygon(); printf ("\nGive Colour Number to Draw Polygon :"); scant ("8d", épolycol) ; printf ("\nGive Colour Number to Fill Polygon :"); scant ("8d", &fillcol); print£("\nEnter a seed point "); scanf ("$d $d", éseedx, éseedy) ; initgraph (sad, sgm,"") ; draw_polygon (polyl, polycol) ; /*** Working area Calculation **#/ for (i=1; i<=polyl.no_of side; i++) ‘ if(polyl.x1[i] maxx) maxx=polyl. x1 [i]; if(polyl.y1 [i] > maxy) maxy=polyl.yl[il; } circle (seedx, seedy, RED) ; getch(); /*** Boundary Fill Filling Process **#/ do { if (seedy==miny) ( dir_flagl =- seedx=nseed: seedy=nseedy-1; fil_flag=-1; } if (seedy==maxy) break; while(£il_flag==1) { if (getpixel (seedx, seedy) == polycol) { f£il_flag = - seedx--; break; ) putpixel (seedx, seedy, fillcol); seedx++; 1 if ((dir_flagl == 1) && (seedy !=miny)) seedy; if ((dir_flagl ==-1) && (seedy ! seedy++; maxy)) while(fil_flag = { if (getpixel (seedx, seedy) == polycol) { -1) fil_flag= seedx++; break; } putpixel (seedx, seedy, fillcol); seedx--; ) }while (1); getch(); 133 } ee 134 gon with Patterns are needed to be filled-in with a pattern ing Many times polygons i wrany ep such situations, the methods discussed in the earlier g cannot be used. Fill with raster displays known as pattern fil repetitively, side-by-side all In flood fill algorithm, we change the colour of all the pixels which arg 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 necordingly. it will be coloured) For this we need, predefined patter + matrices. Some of the patterns are shown in Figure 4.24. 4.3.4 Filling Poly: tead of : ect nga polygon with a given pattern is a very easy ction ‘The method of filling a polygon with a pattern is i, Pattern fill is nothing but tiling a predefined patie: | over the interior part of the polygon.” 00011000 11110000 10000000 eine 00011000 11110000 T111d11dt rr 00111100 11110000 00001000 rate TTTNdit 11110000 00001000 eee Tittiitd oooo1111 00001000 aoe 00111100 ooo0o11II 1ddidiid 00011000 00001111 10000000 00110000 oooo11t 10000000 FIGURE 4.24 Various patterns and their frame buffer oe Pattern fill consider a smallest rectangle within which te revesn {ts Perfectly. Now take the first pixel from the pattern mati polygon, then om wel Of the rectangle and if that pixel is inside the first pixel of the I the details of the first pixel from pattern matrix matrix and for all qe oot: Do this process for all the pixels of patter ¥ all the pixels of the given polygon. Figures 4.2 * 425(a) and (b) show the pattern fill process and the pattern- (a) The filling Process (b) The pattern used FIGURE 4.25 Pattern fill. Polygons oe = seman 135 The following Program 4.10 is for pattern fill process PROGRAM 4.10 Program for filling a polygon with a pattern fill process /*** Program for Filling a Polygon with a Pattern fill process »**/ #include #include #include "c:\tc\include\mygraph.h" main() { int gd=DETECT, gm; int i,j,k, £illcol,polycol, pat_mat {10} [10 int minx=9999,miny=9999,maxx=0,maxy polygon polyl; elrscr(); polyl = read_polygon()i printf ("\nGive Colour Number to Draw Polygon :" scanf ("%d", gpolycol) print£("\nGive Colour Number to Fill Polygon :")i scanf("8a", 6fillcol) ; printf ("\nEnter size of the Pattern Matrix : scanf ("$d $d", gpat_x, &pat_y) i printf ("\nEnter Pattern in 0 and 1 ONLY \n"); for (i=0;i maxx) maxx=polyl. x1 [4] if (polyl.y1[il > maxy) maxy=polyl.yl [ili ) /*** pattern Filling Process ***/ for (i=miny+1;i

You might also like