Algorithm Description:
The algorithm steps of the 4-connected boundary filling algorithm for the stack structure are:
Seed pixel into the stack, repeat the following three steps when the stack is not empty:
(a) stack top pixel stack;
(b) Place the stack pixel into a fill color;
(c) Check out the 4-adjacency point of the stack pixel, if one of the pixels is not a boundary color and is not set to polygon color, then the pixel into the stack.
Comments: Code written by the comparison, using C, but also mixed with C + + templates and STL, horizontal or missing, and the seed in advance fixed (100,100), the biggest disadvantage is that the algorithm is less efficient
Code
1 //seed Region filling algorithm. CPP: The entry point that defines the console application. 2 //3 4#include"stdafx.h"5#include <stdio.h>6#include"Graphics.h"7#include <stdlib.h>8#include <stack>9 Ten using namespacestd; One A //defining structure body storage pixel coordinates - struct Point - { the intx; - inty; - }; - + //function Declaration - voidBoundaryfilling (Point a[],intn); + voidJudgecolor (intXintY, stack<int> &sx, stack<int> &sy); A at intMain () - { - intGdriver =DETECT, Gmode, N, I; - -printf"Please input number of point:\n"); -scanf_s ("%d", &n); in -Point *p= (Point *)malloc(nsizeof(point));//Dynamically allocating memory to +printf"Please input point: \ n"); - for(i =0; I < n; i++) thescanf_s ("%d%d", &p[i].x, &p[i].y); * $Initgraph (&gdriver, &gmode,"");Panax Notoginseng - SetColor (BLUE); the SetBkColor (BLACK); + A //Draw a polygon the for(i =0; I < n1; i++) +Line (p[i].x, P[I].Y, P[i +1].x, P[i +1].y); - $ boundaryfilling (P, n); $ -System"Pause"); - closegraph (); the - return 0;Wuyi } the - voidBoundaryfilling (Point a[],intN) Wu { -stack<int>Sx,sy; About intx=0, y=0, X0,y0,i; $ - for(i =0; I < n; i++) - { -X + =a[i].x; AY + =a[i].y; + } the -x = x/(n1); $y = y/(n1); the theSx.push (x);//x-coordinate into the stack theSy.push (y);//y-coordinate into the stack the - while(!sx.empty ())//determine if the stack is empty in { thex0 =sx.top (); theY0 =sy.top (); About thePutpixel (Sx.top (), Sy.top (), YELLOW);//stack top element coloring theSx.pop ();//stack top element out stack theSy.pop ();//stack top element out stack + -Judgecolor (X0-1, y0, SX, SY);//left Point theJudgecolor (x0 +1, y0, SX, SY);//Right PointBayiJudgecolor (x0, y0-1, SX, SY);//Bottom Point theJudgecolor (x0, y0 +1, SX, SY);//Top Point the } - } - the //determine if the pixel is not shaded the voidJudgecolor (intXinty,stack<int> &sx,stack<int> &Sy) the { the if(GetPixel (x, y) = =BLACK) - { the Sx.push (x); the Sy.push (y); the }94}
The seed filling algorithm of graph fill