码迷,mamicode.com
首页 > 编程语言 > 详细

种子填充算法

时间:2016-05-13 23:23:33      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:

  1 /*************************************************************
  2      pb-图形学题4
  3      种子填充算法
  4 
  5 *************************************************************/
  6 
  7 
  8 #include <GL/glut.h>
  9 #include<cstdio>
 10 #include<cmath>
 11 #include<stack>
 12 using namespace std;
 13 
 14 int x,y,endx,endy,p;
 15 int v[3000][3000];
 16 
 17 struct ST
 18 {
 19     int x,y;
 20 };
 21 ST st[100];
 22 int len=0;
 23 ST f,r;
 24 stack<ST>s;
 25 int yi[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
 26 
 27 void init()
 28 {
 29     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);                 //什么单缓存的东西,现在不懂什么意思,就当通用条件
 30     glutInitWindowPosition(100, 100);                            //图片出现的位置
 31     glutInitWindowSize(400, 300);                                //图片的长和宽
 32     glutCreateWindow("pb-图形学题4");                            //图片的名字
 33 
 34     glClearColor(0.0, 0.0, 0.0, 0.0);
 35     glMatrixMode(GL_PROJECTION);
 36     gluOrtho2D(-1000, 1000, -1000, 1000); 
 37 
 38     glRectf(-0.5f, -0.5f, 0.5f, 0.5f); 
 39     glClear(GL_COLOR_BUFFER_BIT);
 40     glColor3f(1.0, 0.0, 0.0); 
 41     
 42     
 43     memset(v,0,sizeof(v));
 44     while (!s.empty()) s.pop();
 45 }
 46 
 47 
 48 void setPixel (GLint x,GLint y)               //描点函数,表示从x,y到nx,ny的矩形全部涂色
 49 {
 50     glBegin(GL_POINTS);
 51        glVertex2i(x, y);
 52     glEnd();
 53 }
 54 
 55 //交换坐标。
 56 void swap(int &x,int &y)
 57 {
 58     x^=y;
 59     y^=x;
 60     x^=y;
 61 }
 62 
 63 
 64 //求k得范围,p=1表示 0<k<1; p=2表示 k>1; p=3 表示 -1<k<0; p=4表示 k<-1;
 65 //并把初始坐标映射的0<k<1。
 66 void Spoint()
 67 {
 68    if ((endx-x)*(endy-y)>0)
 69     {
 70         if (abs(endx-x)-abs(endy-y)>=0) p=1;
 71         else 
 72         {
 73             p=2;
 74             swap(x,y);
 75             swap(endx,endy);
 76         }
 77     }
 78     else
 79     {
 80            if (abs(endx-x)-abs(endy-y)>=0) 
 81            {
 82                p=3;
 83                y=-y;
 84                endy=-endy;
 85            }
 86            else 
 87            {
 88                p=4;
 89                x=-x;
 90                endx=-endx;
 91                swap(x,y);
 92                swap(endx,endy);
 93            }
 94     }
 95 }
 96 
 97 
 98 
 99 
100 //根据k判断对称范围,再描点。
101 void setpoint(int x0,int y0)
102 {
103     if (p==1)  
104     {
105         v[x0+1001][y0+1001]=1;
106     //    setPixel(x0,y0);
107     }
108     if (p==2)  
109     {
110         
111         v[y0+1001][x0+1001]=1;
112     //    setPixel(y0,x0);
113     }
114     if (p==3)  
115     {
116     
117         v[x0+1001][-y0+1001]=1;
118     //    setPixel(x0,-y0);
119     }
120     if (p==4)  
121     {
122     
123         v[-y0+1001][x0+1001]=1;
124     //    setPixel(-y0,x0);
125     }
126 }
127 
128 //Bresenham算法。
129 void myDisplay(void)                                             
130 { 
131     x=st[len-2].x;
132     y=st[len-2].y;
133     int i;
134     for (i=0;i<len-1;i++)
135     {
136         endx=st[i].x;
137         endy=st[i].y;
138         Spoint();
139         int dx=abs(x-endx),dy=abs(y-endy);
140         int d=2*dy-dx;
141         int tdy=2*dy,tdx=2*(dy-dx);
142         
143         if (x>endx)
144         {
145            swap(x,endx);
146            swap(y,endy);
147         }
148         x-=40;
149         v[x+1001][y+1001]=1;
150         
151         while (x<endx+40)
152         {
153             x++;
154             if (d<0)
155             {
156                 d+=tdy;
157             }
158             else
159             {
160               y++;
161              d+=tdx;
162             }
163           setpoint(x,y);
164          
165         }
166         x=st[i].x;
167         y=st[i].y;
168     }
169  glFlush();
170     f.x=0;
171     f.y=0;
172     s.push(f);
173 
174     while (!s.empty())
175     {
176         r=s.top();
177         s.pop();
178         setPixel(r.x,r.y);
179         for (i=0;i<4;i++)
180         {
181             f.x=r.x+yi[i][0];
182             f.y=r.y+yi[i][1];
183             if (f.x<=-1000||f.x>=1000) continue;
184             if (f.y<=-1000||f.y>=1000) continue;
185             if (v[f.x+1001][f.y+1001]) continue;
186             v[f.x+1001][f.y+1001]=1;
187             s.push(f);
188         }
189     }
190      glFlush();
191                                                      
192  }
193 
194 
195 
196  int main(int argc, char *argv[])
197  {
198      glutInit(&argc, argv);
199      while (~scanf("%d%d",&st[len].x,&st[len++].y));
200 
201      init();                                                     //初始化数据
202      glutDisplayFunc(&myDisplay);                                //调用函数                    
203      glutMainLoop();                                             //开始程序
204      return 0; 
205  }

 

种子填充算法

标签:

原文地址:http://www.cnblogs.com/pblr/p/5491309.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!