小弟现在在写一个图形学中的种子填充程序:代码如下:
#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);
}
结果总是不能全部的显示啊。不知道错在那里?谢谢了先!!!
解决方案 »
- 怎么填充一个数组?
- 改变文本框中文字的颜色???
- 求解?使用_ATL_DEBUG_INTERFACES导致错误的问题
- 如何将在ListCtrl修改后的内容保存原来的文件中。
- WSAGetLastError ()函数返回值是10054,请问是何错误,怎么解决啊;跟贴有分
- 怎么样让程序自动搜索一下局域网中的在线用户,急啊,
- ★☆★300分狂送高人:每秒要处理100帧图像,把768*288的单场图像数据放大成768*576全帧图像,有没有快速算法?
- [求助]Navigate2中如何实现修改header参数来伪造referrer
- 为什么位图叠加文字后,字符是反的,是什么原因?
- 用SDK开发的程序如何读取访问ACCESS、SQL等数据库?
- 紧急招聘!!!MFC强手好机会!!!
- 想用openssl写个客户端程序,用于模拟浏览器,访问https的网站
/*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的正负。
感觉四个if不能同时用啊。这是怎么回事啊?知道的说啊。非常谢谢!
有人知道的说一下,在线等。
是不是堆栈用错了啊?我找不到啊。