提前的配置和8连通的(上一篇相同)
4连通的代码如下:
#include
#include
typedef float Color[3];
rgbColorEqual(Color c1,Color c2)
{
if(abs(c1[1]-c2[1])<0.001 && abs(c1[2]-c2[2])<0.001 && abs(c1[0]-c2[0])<0.001)
return true;
else
return false;
}
void setPixel(GLint x, GLint y)
{
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
void getPixel(GLint x, GLint y, Color c)
{
glReadPixels(x,y,1,1,GL_RGB,GL_FLOAT,c);
}
void BoundaryFill8(int x, int y,Color fillColor,Color borderColor)
{
Color currentColor;
getPixel(x,y,currentColor);
if((!rgbColorEqual(currentColor,fillColor))&&(!rgbColorEqual(currentColor,borderColor)))
{
//setColor(fillColor);
setPixel(x,y);
BoundaryFill4( x+1, y, fillColor, borderColor);
BoundaryFill4( x-1, y, fillColor, borderColor);
BoundaryFill4( x, y+1, fillColor, borderColor);
BoundaryFill4( x, y-1, fillColor, borderColor);
}
}
void init(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode (GL_PROJECTION);
gluOrtho2D (0.0, 200.0, 0.0, 200.0);
}
void Draw(void)
{
Color a={1.0,0.0,0.0},b={0.0,1.0,1.0};
glColor3fv(b);
glClear(GL_COLOR_BUFFER_BIT);
//设置边界线宽,否则填充时会溢出
glLineWidth(4.0);
//绘制多边形区域
glBegin(GL_LINE_LOOP);
glVertex2i(90, 40);
glVertex2i(120, 100);
glVertex2i(90, 160);
glVertex2i(60, 160);
glVertex2i(60, 40);
glEnd();
glColor3fv(a);
BoundaryFill8(70,60,a,b);
glFlush();
}
void main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(200, 200);
glutCreateWindow("4连通边界填充算法!");
init();
glutDisplayFunc(Draw);
glutMainLoop();
}
程序执行完则显示填充完整个多边形