运用八连通边界填充算法进行多边形的填充
(1)首先通过颜色混合的方式形成一个图形包中函数的对多边形的填充,如下图所示:
【注】主要代码如下://实现多边形的简单填充
#include
#ifdef __APPLE__
#include
#else
#include
#endif
#include
void init (void)
{
glClearColor (1.0, 1.0, 1.0, 0.0); // Set display-window color to white.
glMatrixMode (GL_PROJECTION); // Set projection parameters.
gluOrtho2D (0.0, 200.0, 0.0, 150.0);
}
void lineSegment (void)
{
glClear (GL_COLOR_BUFFER_BIT); // Clear display window.
glColor3f (0.0, 0.0, 1.0); // Set line segment color to red.
// glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glBegin (GL_POLYGON);
glShadeModel(GL_TRIANGLES);
glVertex2i(20,40);
glVertex2i(25,60);
glVertex2i(30,70);
glVertex2i(40,80);
glColor3f(1.0,0.0,0.0);
glVertex2i(30,90);
glVertex2i(20,75);
glEnd ( );
glFlush ( ); // Process all OpenGL routines as quickly as possible.
}
int main (int argc, char** argv)
{
glutInit (&argc, argv); // Initialize GLUT.
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // Set display mode.
glutInitWindowPosition (50, 100); // Set top-left display-window position.
glutInitWindowSize (400, 300); // Set display-window width and height.
glutCreateWindow ("An Example OpenGL Program"); // Create display window.
init ( ); // Execute initialization procedure.
glutDisplayFunc (lineSegment); // Send graphics to display window.
glutMainLoop ( ); // Display everything and wait.
return 0;
}
(2)通过8连通边界填充算法进行填充多边形,多边形边界使用4倍线宽。
【注】主要代码如下:
void BoundaryFill8(int x, int y,Color fillColor,Color borderColor)
{
//在此添加4连通或8连通代码
Color interColor;
//glReadPixels(x,y,1,1,GL_RGB,GL_UNSIGNED_INT,color);
getPixel(x,y,interColor);
if (!rgbColorEqual(interColor,borderColor) && !rgbColorEqual(interColor,fillColor)){
setPixel(x,y);
BoundaryFill8(x+1,y,fillColor,borderColor);
BoundaryFill8(x-1,y,fillColor,borderColor);
BoundaryFill8(x,y+1,fillColor,borderColor);
BoundaryFill8(x,y-1,fillColor,borderColor);
BoundaryFill8(x+1,y+1,fillColor,borderColor);
BoundaryFill8(x-1,y-1,fillColor,borderColor);
BoundaryFill8(x-1,y+1,fillColor,borderColor);
BoundaryFill8(x-1,y-1,fillColor,borderColor);
}
}
(3)通过4连通洪泛填充算法进行填充多边形,多边形边界使用4倍线宽。
【注】主要代码如下:
//实现多边形的简单填充
#include
#ifdef __APPLE__
#include
#else
#include
#endif
#include
#include
typedef float Color[3];
bool rgbColorEqual(Color c1,Color c2)
{
if(fabs(c1[1]-c2[1])<0.001 && fabs(c1[2]-c2[2])<0.001 && fabs(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 BoundaryFill4(int x, int y,Color fillColor,Color borderColor)
{
//在此添加4连通或8连通代码
Color interColor;
//glReadPixels(x,y,1,1,GL_RGB,GL_UNSIGNED_INT,color);
getPixel(x,y,interColor);
if (!rgbColorEqual(interColor,borderColor) && !rgbColorEqual(interColor,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 FloudFill4(int x, int y,Color fillColor,Color initColor)
{
//在此添加4连通或8连通代码
Color interColor;
//glReadPixels(x,y,1,1,GL_RGB,GL_UNSIGNED_INT,color);
getPixel(x,y,interColor);
if (rgbColorEqual(interColor,initColor) && !rgbColorEqual(interColor,fillColor)){
setPixel(x,y);
FloudFill4(x+1,y,fillColor,initColor);
FloudFill4(x-1,y,fillColor,initColor);
FloudFill4(x,y+1,fillColor,initColor);
FloudFill4(x,y-1,fillColor,initColor);
}
}
void BoundaryFill8(int x, int y,Color fillColor,Color borderColor)
{
//在此添加4连通或8连通代码
Color interColor;
//glReadPixels(x,y,1,1,GL_RGB,GL_UNSIGNED_INT,color);
getPixel(x,y,interColor);
if (!rgbColorEqual(interColor,borderColor) && !rgbColorEqual(interColor,fillColor)){
setPixel(x,y);
BoundaryFill8(x+1,y,fillColor,borderColor);
BoundaryFill8(x-1,y,fillColor,borderColor);
BoundaryFill8(x,y+1,fillColor,borderColor);
BoundaryFill8(x,y-1,fillColor,borderColor);
BoundaryFill8(x+1,y+1,fillColor,borderColor);
BoundaryFill8(x-1,y-1,fillColor,borderColor);
BoundaryFill8(x-1,y+1,fillColor,borderColor);
BoundaryFill8(x-1,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,1.0,0.0},b={0.0,1.0,1.0};
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(4.0);
glViewport(0,0,200,200);
glColor3fv(b);
glBegin(GL_LINE_LOOP);
glVertex2i(90, 40);
glVertex2i(120, 100);
glVertex2i(90, 160);
glVertex2i(60, 160);
glVertex2i(60, 40);
glEnd();
glColor3fv(a);
BoundaryFill4(70,60,a,b);
glViewport(200,0,200,200);
glColor3fv(b);
glBegin(GL_POLYGON);
glVertex2i(90, 40);
glVertex2i(120, 100);
glVertex2i(90, 160);
glVertex2i(60, 160);
glVertex2i(60, 40);
glEnd();
glColor3fv(a);
FloudFill4(70,60,a,b);
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 200);
glutCreateWindow("4连通边界和洪泛填充算法!");
init();
glutDisplayFunc(Draw);
glutMainLoop();
return 0;
}