Image To PDF 20240124 23.04.14
Image To PDF 20240124 23.04.14
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 oooPolygons 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 examinea 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 ganproGRAM 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 PointsComputer 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=lyi arrx[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 (iliCompute 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 } ee134 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
Understanding Polygon Types and FillingNo ratings yetUnderstanding Polygon Types and Filling15 pages Different Types of Polygons - Simple Convex - Simple Concave - Non-Simple: Self-Intersecting - With HolesNo ratings yetDifferent Types of Polygons - Simple Convex - Simple Concave - Non-Simple: Self-Intersecting - With Holes38 pages 2IV60 Computer Graphics Graphics Primitives and Attributes: Jack Van Wijk TU/eNo ratings yet2IV60 Computer Graphics Graphics Primitives and Attributes: Jack Van Wijk TU/e45 pages Understanding Reflex Vertices in PolygonsNo ratings yetUnderstanding Reflex Vertices in Polygons53 pages Polygons in Computer Graphics TechnologyNo ratings yetPolygons in Computer Graphics Technology39 pages Polygon Math Education Presentation in Colorful Abstract StyleNo ratings yetPolygon Math Education Presentation in Colorful Abstract Style10 pages