种子填充错在那啊?
小弟现在在写一个图形学中的种子填充程序:代码如下:
#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);
}
结果总是不能全部的显示啊。不知道错在那里?谢谢了先!!!
#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);
}
结果总是不能全部的显示啊。不知道错在那里?谢谢了先!!!
...全文
相关推荐
0371、新一代单片PFC+PWM控制器.zip
0371、新一代单片PFC+PWM控制器.zip
新一代单片PFC+PWM控制器.zip
新一代单片PFC+PWM控制器.zip
【单片机-嵌入式-stm32项目资料】新一代单片PFC+PWM控制器.zip
【单片机-嵌入式-stm32项目资料】新一代单片PFC+PWM控制器.zip
单片机-新一代单片PFC+PWM控制器.zip
单片机-新一代单片PFC+PWM控制器.zip
Java编写图形学的种子填充算法
用C写的图形学填充算法已经很多了,看到不少帖子都是在问关于如何用Java编写图形学的填充算法,说来也巧,我刚好要做一个这个方面的实验,用的是扫描线种子填充算法,由于时间仓促,代码质量可能不算很高,希望大家见谅,并希望大家都来为我指点一下,小弟不胜感激~~好了,废话就不说了,我们先一起来看一下相关的知识~扫描线种子填充算法思想 首先填充种子所在的尚未填充的一区段,然后确定与这一区段相邻



