小弟现在在写一个图形学中的种子填充程序:代码如下:
#include<graphics.h>
#include "math.h"
#define STACK_INIT_SIZE 1024
#define STACKINCREMENT 10
#define LEN sizeof(struct PixelType)
/*struct for Stack */
 struct SqStack
{
struct PixelType * base;
struct PixelType * top;
int stacksize;
};
/*element */
 struct PixelType
{
struct Dot * pixeldot;
short flag;
};
/*dot*/
 struct Dot
 {
 int x;
 int y;
 };
int InitStack(struct SqStack * S)
{
/* */
S->base=( struct PixelType *)malloc(STACK_INIT_SIZE * LEN);
if(!S->base)
return 0;
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return 1;
}
int StackEmpty(struct SqStack *S)
{
if(S->top==S->base)
return 1;
return 0;
}
int Push(struct SqStack *S,struct PixelType e)
{
if(S->top-S->base>=S->stacksize)
{
S->base=(struct PixelType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*LEN);
if(!S->base)
return 0;
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}
*S->top=e;
S->top++;
return 1;
}
struct PixelType Pop(struct SqStack *S)
{
struct PixelType e;
   e=*--S->top;
   return e;
}
/*Give the coordx,coordy and radius,and x,y. To judge the range of x and y */
int Indistance(int x0,int y0,int r0,int x,int y,int x1,int x2,int y1,int y2)
{
long fg=(x-x0)*(x-x0)+(y-y0)*(y-y0)-r0*r0;
if(((x1<x)&&(x<x2))&&((y1<y)&&(y<y2)))
return 1;
else if((x>x2)||(x<x1)||(y>y2)||(y<y1))
return 0;
else if(((x==x2)&&(y==y0))||((x==x1)&&(y==y0))||((x==x0)&&(y==y1))||((x==x0)&&(y==y2)))
return 0;
else
if(fg<0)
return 1;
else
return 0;
}
void SeedFill(int x0,int y0,int r0)
{
int color;
/*Give the range of the circle*/
int x1,x2,y1,y2;
int x,y;
struct SqStack * seedstack;
struct PixelType  Pixel0;
struct PixelType seedpixel; color=RED;
x=y=0;
seedpixel.pixeldot->x=x0;
seedpixel.pixeldot->y=y0;
/* hava no set color */
InitStack(seedstack);
Push(seedstack,seedpixel);
x1=x0-(sqrt(2)/2)*r0;
x2=x0+(sqrt(2)/2)*r0;
y1=y0-(sqrt(2)/2)*r0;
y2=y0+(sqrt(2)/2)*r0;
while(!StackEmpty(seedstack))
{
Pixel0=Pop(seedstack);
/*200,200*/
x=Pixel0.pixeldot->x;
y=Pixel0.pixeldot->y;
putpixel(x,y,color);
/*201,200*/
if(getpixel(x+1,y)!=color&&Indistance(x0,y0,r0,x+1,y,x1,x2,y1,y2))
{
Pixel0.pixeldot->x=x+1;
Pixel0.pixeldot->y=y;
Push(seedstack,Pixel0);
}
/*200,201*/
if(getpixel(x,y+1)!=color&&Indistance(x0,y0,r0,x,y+1,x1,x2,y1,y2))
{
Pixel0.pixeldot->x=x;
Pixel0.pixeldot->y=y+1;
Push(seedstack,Pixel0);
}
/*199,200*/
if(getpixel(x-1,y)!=color&&Indistance(x0,y0,r0,x-1,y,x1,x2,y1,y2))
{
Pixel0.pixeldot->x=x-1;
Pixel0.pixeldot->y=y;
Push(seedstack,Pixel0);
}
/*200,199*/
if(getpixel(x,y-1)!=color&&Indistance(x0,y0,r0,x,y-1,x1,x2,y1,y2))
{
Pixel0.pixeldot->x=x;
Pixel0.pixeldot->y=y-1;
Push(seedstack,Pixel0);
}
}/*while*/
}
void main()
{
 int radius,coordx,coordy;
 int graphmode,graphdriver=DETECT;
 initgraph(&graphdriver,&graphmode,"...bgi");
 coordx=coordy=radius=200;
 circle(coordx,coordy,radius);
 SeedFill(coordx,coordy,radius);

结果总是不能全部的显示啊。不知道错在那里?谢谢了先!!!

解决方案 »

  1.   

    我用的是4连通。邻域判断用的是下面函数:
    /*Give the coordx,coordy and radius,and x,y. To judge the range of x and y */
    int Indistance(int x0,int y0,int r0,int x,int y,int x1,int x2,int y1,int y2)
    {
       long fg=(x-x0)*(x-x0)+(y-y0)*(y-y0)-r0*r0;   if(((x1<x)&&(x<x2))&&((y1<y)&&(y<y2)))
            return 1;
       else if((x>x2)||(x<x1)||(y>y2)||(y<y1))
            return 0;
       else if(((x==x2)&&(y==y0))||((x==x1)&&(y==y0))||((x==x0)&&(y==y1))||((x==x0)&&    (y==y2)))
           return 0;
       else
           if(fg<0)
                return 1;
           else
                return 0;
    }区域是圆形。fg=(x-x0)*(x-x0)+(y-y0)*(y-y0)-r0*r0;
    判断fg的正负。
      

  2.   

    是不是我的四连域会造成内存堆栈的溢出啊?我的while语句错了吗?
    感觉四个if不能同时用啊。这是怎么回事啊?知道的说啊。非常谢谢!
      

  3.   

    怎么没人帮我看看啊。程序说我 NULL pointer assigment  我不知道我错在那里?
    有人知道的说一下,在线等。
    是不是堆栈用错了啊?我找不到啊。