C语言实现多边形填充
楼主问的是图形学算法。。1楼给出的是??
有以前写了一个多边形种子填充算法用的是贝塞尔方法你拿去看下把。。
1 用Bresenham直线与圆组成的扇形
void CFill4Dlg::Bresenham(int x0,int y0,int x1,int y1,int color)
{
int x,y,dx,dy,e;
dx=y1-x0;
dy=y1-y0;
e=-dx;
x=x0;
y=y0;
CClientDC dc(this);
for(x=x0;xx1;x++)
{
dc.SetPixel(x,(int)(y+0.5),color);
e=e+2*dy;
if(e=0)
{
y++;
e=e-2*dx;
}
}
}
void CFill4Dlg::CirclePoints(int x,int y,int m,int n,int color)
{
CClientDC dc(this);
dc.SetPixel(x+m_cx,y+m_cy,color);
dc.SetPixel(y+m_cx,x+m_cy,color);
dc.SetPixel(-x+m_cx,y+m_cy,color);
dc.SetPixel(y+m_cx,-x+m_cy,color);
dc.SetPixel(x+m_cx,-y+m_cy,color);
dc.SetPixel(-y+m_cx,x+m_cy,color);
dc.SetPixel(-x+m_cx,-y+m_cy,color);
dc.SetPixel(-y+m_cx,-x+m_cy,color);
}
void CFill4Dlg::MidPointCircle(int r,int m,int n,int color)
{
int x,y;
float d;
x=0;
y=r;
d=float(1.25-r);
CirclePoints(x,y,m,n,color);
while(x=y)
{
if(d0)
d+=2*x+3;
else
{
d+=2*(x-y)+5;
y–;
}
x++;
CirclePoints(x,y,m,n,color);
}
}
2 填充:
种子设定 filled_color=getpixel(m_zx,m_zy);
typedef struct
{//记录种子点
int x;
int y;
}seed;
seed * seed_p;
#define STACKTOTAL 3000
void stack_init();
void setstackempty();
void stackpush(seed pt);
seed stackpop();
bool isstackempty();
int stack_number;
填充:
void CFill4Dlg::FloodFill4(int x,int y,int oldcolor,int newcolor)
{
CClientDC dc(this);
if((int)dc.GetPixel(x,y)==oldcolor)
{
drawpixel(x,y,newcolor);
FloodFill4(x,y+1,oldcolor,newcolor);
FloodFill4(x,y-1,oldcolor,newcolor);
FloodFill4(x-1,y,oldcolor,newcolor);
FloodFill4(x+1,y,oldcolor,newcolor);
}
}
void CFill4Dlg::drawpixel(int x, int y, int color)
{
CClientDC dc(this);
dc.SetPixel(x,y,color);
}
比特填充和删除的算法(C语言实现)
int input() //写这个函数用于按顺序一个一个读取比特,返回 0,1和-1,-1表示到文件尾
//然后申请一个足够大的地址用于写入数据
int insert(int) //用这个函数写入数据
//逻辑代码如下
int result = input();
int count = 5;
while(result != -1)
{
insert(result);
if(result == 1)
count–;
else
count = 5;
if(count == 0)
{
insert(0);
count = 5;
}
}
C语言 矩形填充算法
#include stdio.h
#include graphics.h
typedef struct {
int xmin, xmax, ymin, ymax;
} Rectangle;
void FillRectangle(Rectangle *rect, int color) {
int x = 0, y = 0;
for (y = rect-ymin; y = rect-ymax; y++)
for (x = rect-xmin; x = rect-xmax; x++) //这里有个分号,应该去掉。
putpixel(x, y, color);
}/* end of FillRectangle() */
int main() {
//declare our color
int color = 0;
//declare our rectangle
Rectangle *rect = (Rectangle *) malloc(sizeof(Rectangle));
if(NULL == rect )
{
printf(“allocation memory failed!\n”);
return 1;
}
//input the scope
printf(“Enter the x-min:\n”);
scanf(“%d”, rect-xmin);
printf(“Enter the x-max:\n”);
scanf(“%d”, rect-xmax);
printf(“Enter the y-min:\n”);
scanf(“%d”, rect-ymin);
printf(“Enter the y-max:\n”);
scanf(“%d”, rect-ymax);
//input the color
printf(“Enter your color:\n”);
scanf(“%d”, color);
//call our paint function
FillRectangle(rect, color);
return 0;
}
求一个C语言实现的种子填充多边形算法程序
/*如果是用线填充,程序如下。如果是用点填充需要用到堆栈和系统底层库函数或者用画点函数putpixel()。 下面实例是用扫描线填充长方形,开始要输入长方形的左上顶点坐标和右下顶点坐标以及填充扫描线的间距(=1),如果间距等于1,就是完全填充(实填充)。 一个完整的c程序如下,程序在win-tc和tc2.0下都调试通过。 */ #includestdio.h #includestdlib.h #includeconio.h #includegraphics.h void draw(int x1,int y1,int x2,int y2,int delta) {int nx1,ny1,nx2,ny2; nx1=x1,ny1=y2-delta,nx2=x1+delta,ny2=y2; while((ny1=y1)(nx2=x2)) {line(nx1,ny1,nx2,ny2); ny1-=delta; nx2+=delta; } if(nx2x2) {ny2-=nx2-x2; nx2=x2; while(ny1y1) {line(nx1,ny1,nx2,ny2); ny1-=delta; ny2-=delta; } nx1+=y1-ny1; ny1=y1; while(nx1x2) {line(nx1,ny1,nx2,ny2); nx1+=delta; ny2-=delta; } } else {nx1+=y1-ny1; ny1=y1; while(nx2x2) {line(nx1,ny1,nx2,ny2); nx2+=delta; nx1+=delta; } ny2-=nx2-x2; nx2=x2; while(ny2y1) {line(nx1,ny1,nx2,ny2); ny2-=delta; nx1+=delta; } } } int main(void) {int x1,y1,y2,x2,delta; int driver=DETECT,mode; printf(“Please input lefttop(x1,y1) and rightbottom(x2,y2) of rectangle and delta:\n”); scanf(“%d%d%d%d%d”,x1,y1,x2,y2,delta); initgraph (driver,mode,”C:\\TC”); /*这里*/ rectangle(x1,y1,x2,y2); draw(x1,y1,x2,y2,delta); gotoxy(1,1); printf(“Press any key to exit!”); getch(); closegraph(); return 0; } /*说明:将main()函数中的initgraph(gdriver,gmode,””);中的””更改为你的TC安装目录,一般tc必须安装在c盘根目录下,所以就是initgraph(gdriver,gmode,”C:\\TC”);如你的TC安装目录为D盘的Tools目录下的TC目录,那么上述语句改为: initgraph(gdriver,gmode,”D:\\Tools\\TC”); 同时保证在D:\\Tools\\TC目录里有文件EGAVGA.BGI,万一不行,将本程序复制到你的TC安装目录下再运行。 */
急求用C语言编写的扫描线填充多边形的算法
这是从书上第99页节录下来的,试试吧!
#define SET_BIT_MACRO( a , b ) if( image[a][b] == HIGH){ \
image[a][b] = color; cnt++; }
/**
* image: 影像阵列
* xs, ys: 开始位置
* color: 比对的颜色
*/
void pixelset(unsigned char image[Y_SIZE][X_SIZE], int xs, int ys, int color){
int i, j, cnt, im, ip, jm, jp;
image[ys][xs] = color;
while( cnt != 0 ){
cnt = 0;
for(i = 0; i Y_SIZE; i++){
for(j = 0; j X_SIZE; j++){
if(image[i][j]==color){
im=i-1; ip=i+1; jm=j-1; jp=j+1;
if(im 0) im=0; if(ipY_SIZE-1) ip=Y_SIZE-1;
if(jm 0) jm=0; if(jpX_SIZE-1) jp=X_SIZE-1;
// 周围八个方向都找一遍
SET_BIT_MACRO( image[i][jp] )
SET_BIT_MACRO( image[im][jp] )
SET_BIT_MACRO( image[im][j] )
SET_BIT_MACRO( image[im][jm] )
SET_BIT_MACRO( image[i][jm] )
SET_BIT_MACRO( image[ip][jm] )
SET_BIT_MACRO( image[ip][j] )
SET_BIT_MACRO( image[ip][jp] )
}
}
}
}
}