4连通边界填充算法

 提前的配置和8连通的(上一篇相同)

4连通的代码如下:

 #include <GL/glut.h>
 #include <math.h>
 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(); 
 }

程序执行完则显示填充完整个多边形

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值