种子填充错在那啊?

Zhentiwei 2006-10-07 08:53:36
小弟现在在写一个图形学中的种子填充程序:代码如下:
#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);
}
结果总是不能全部的显示啊。不知道错在那里?谢谢了先!!!
...全文
249 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Zhentiwei 2006-10-09
  • 打赏
  • 举报
回复
怎么没人帮我看看啊。程序说我 NULL pointer assigment 我不知道我错在那里?
有人知道的说一下,在线等。
是不是堆栈用错了啊?我找不到啊。
Zhentiwei 2006-10-09
  • 打赏
  • 举报
回复
是不是我的四连域会造成内存堆栈的溢出啊?我的while语句错了吗?
感觉四个if不能同时用啊。这是怎么回事啊?知道的说啊。非常谢谢!
Zhentiwei 2006-10-08
  • 打赏
  • 举报
回复
我用的是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的正负。
jixingzhong 2006-10-07
  • 打赏
  • 举报
回复
你用的是 4/8 连域?

邻域判断 没有问题么 ?

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧