Seed filling is actually very simple, computer graphics introduced in the use of stacks, feel the author is not brain water, direct use of a queue with a wide search on it, ah, but I do not bother to write, direct a recursive forget, interested students to try their own
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>
int graph[500][500];
void Scanline_seed_fill (int color,int sx,int sy)
{
Graph[sx][sy] = 1;
Putpixel (SX, SY, color);
if (graph[sx+1][sy] = = 0 && sx+1 < 500)
{
Sleep (50);
Scanline_seed_fill (color,sx + 1,sy);
}
if (graph[sx-1][sy] = = 0 &&sx-1 > 0)
{
Sleep (50);
Scanline_seed_fill (Color,sx-1,sy);
}
if (graph[sx][sy+1] = = 0 && sy+1 < 500)
{
Sleep (50);
Scanline_seed_fill (COLOR,SX, sy + 1);
}
if (graph[sx][sy-1] = = 0 && sy-1 > 0)
{
Sleep (50);
Scanline_seed_fill (color,sx,sy-1);
}
}
void Seed_fill (int startx,int starty,int color) {
int Driver=vga,mode=vgahi;
Initgraph (&driver,&mode, "");
SetBkColor (0);
int x;
int y;
Draw graphics
For (x=1,y=50;x<=50 && y>=1;x++,y--)
{
Putpixel (X,y,color);
Graph[x][y]=1;
}
For (x=100,y=1;x<=150 && y<=50;x++,y++)
{
Putpixel (X,y,color);
Graph[x][y]=1;
}
For (x=1,y=100;x<=50 && y<=150;x++,y++)
{
Putpixel (X,y,color);
Graph[x][y]=1;
}
For (x=100,y=150;x<=150 && y>=100;x++,y--)
{
Putpixel (X,y,color);
Graph[x][y]=1;
}
for (x=50;x<=100;x++)
{
Putpixel (X,1,color);
Graph[x][1]=1;
Putpixel (X,150,color);
Graph[x][150]=1;
}
for (y=50;y<=100;y++)
{
Putpixel (1,y,color);
Graph[1][y]=1;
Putpixel (150,y,color);
Graph[150][y]=1;
}
GetChar ();
Scanline_seed_fill (255,startx,starty);
Getch ();
}
int main ()
{
Initgraph (1000,1000); Initialization diagram
Seed_fill (100,100,255);
GetChar (); Pause screen
return 0;
}
Implementation of C + + program for seed filling algorithm in computer graphics