| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1774 人关注过本帖
标题:[求助]求图形填充的代码
只看楼主 加入收藏
whoamiiamye
Rank: 1
等 级:新手上路
帖 子:55
专家分:0
注 册:2006-10-18
收藏
 问题点数:0 回复次数:6 
[求助]求图形填充的代码
请问怎么用扇形填充或蔓延填充来填充一个圆?
搜索更多相关主题的帖子: 图形 代码 扇形 
2006-10-31 14:46
whoamiiamye
Rank: 1
等 级:新手上路
帖 子:55
专家分:0
注 册:2006-10-18
收藏
得分:0 

拿这个圆为例,怎么填充呢。
#include<graphics.h>
#include<string.h>
#include<stdio.h>

void circlePoint(int x,int y)
{
putpixel(x+300,y+300,5);
putpixel(y+300,x+300,5);
putpixel(-y+300,x+300,5);
putpixel(-x+300,y+300,5);
putpixel(-x+300,-y+300,5);
putpixel(-y+300,-x+300,5);
putpixel(y+300,-x+300,5);
putpixel(x+300,-y+300,5);
}
void MidBresenhamcircle(int r)
{
int x,y,d;
x=0;y=r;d=1-r;
while(x<y){
circlePoint(x,y);
if(d<0)d+=2*x+3;
else{
d+=2*(x-y)+5;
y--;
}
x++;
}
}
main()
{
int gdriver=DETECT,gmode,k;
initgraph(&gdriver,&gmode," ");
scanf("%d",&k);
MidBresenhamcircle(k);
getch();

}


2006-10-31 15:29
一笔苍穹
Rank: 1
等 级:新手上路
帖 子:640
专家分:0
注 册:2006-5-25
收藏
得分:0 

帖一段EC中的种子填充代码,网友曾庆伟的作品,希望对你有用:

/* 预定义位置堆栈的大小。100已经是一个很大的数值了,通常不会溢出 */
#ifndef FILL_STACK
#define FILL_STACK 100
#endif

struct point
{
int x;
int y;
};

/* 申请种子填充需要的位置堆栈 */
struct point *call_fill_stack()
{
int i;
struct point *mem = (struct point *)calloc(FILL_STACK, sizeof(struct point));
if(mem == NULL)
{
message(3, svga_info);
}
else
{
for(i=0; i<FILL_STACK; i++)
{
mem[i].x = mem[i].y = -1;
}
}
return(mem);
}

/* 处理每行的操作 */
void fill_process_row(int y , int mode)
{
signed long begin = position(0, y);
if(mode == COPY_GET)
{
one_row(get, border_get, &begin, y, SYS.SEG, SYS.OFF, SYS.LENGTH_OF_ROW);
}
else if(mode == COPY_PUT)
{
one_row(put, border_put, &begin, y, SYS.SEG, SYS.OFF, SYS.LENGTH_OF_ROW);
}
else
{
return;
}
}

/* 探测相邻行是否需要填充,需要的话,把每个区段的最右侧的点压入堆栈 */
void fill_detect(int left, int right, int y, BITS fill_color, BITS edge_color,
struct point max[], int *count)
{
int i;
int flag = 0; /* 0表示从边界恢复,1表示从填充区到达边界 */

if(left<0 || right<0)
{
return;
}

for(i=left; i<=right; i++)
{
if(memcmp(RB+i*PL, edge_color, PL) && memcmp(RB+i*PL, fill_color, PL))
{
flag = 1;
}
else
{
if(flag == 1)
{
(*count)++;
if(*count > FILL_STACK)
{
message(3, svga_info);
*count = -1;
return;
}
else
{
max[*count].x = i-1;
max[*count].y = y;
}
}
flag = 0;
}
}
if(flag == 1)
{
(*count)++;
max[*count].x = right;
max[*count].y = y;
}
}

/* 填充特定行,并将其上下行每段的最右位置写入数组max,作为待填充标志 */
void fill_row(struct point max[], int *count, BITS fill_color, BITS edge_color)
{
int left = 0, right = 0;
int i;
int y = max[*count].y, x = max[*count].x;

if(x<0 || x>SYS.MAX_X || y<0 || y>SYS.MAX_Y)
{
(*count)--;
return;
}
fill_process_row(y, COPY_GET);
/* 检测初始点,如果是边界色或者填充色,直接退出 */
if(memcmp(RB+x*PL, edge_color, PL) && memcmp(RB+x*PL, fill_color, PL))
{
memcpy(RB+x*PL, fill_color, PL);
}
else
{
(*count)--;
return;
}
/* 从初始点向两侧检测边界 */
for(i=x-1; i>=0; i--)
{
if(memcmp(RB+i*PL, edge_color, PL) && memcmp(RB+i*PL, fill_color, PL))
{
memcpy(RB+i*PL, fill_color, PL);
}
else
{
left = i + 1;
break;
}
}
if(i == -1)
{
left = 0;
}

for(i=x+1; i<=SYS.MAX_X; i++)
{
if(memcmp(RB+i*PL, edge_color, PL) && memcmp(RB+i*PL, fill_color, PL))
{
memcpy(RB+i*PL, fill_color, PL);
}
else
{
right = i - 1;
break;
}
}
if(i == SYS.MAX_X+1)
{
right = SYS.MAX_X;
}

fill_process_row(y, COPY_PUT);
(*count)--;
/* 检测本行的上下行,在left到right范围内看是否有需要填充的区段 */
if(y == 0)
{
fill_process_row(y+1, COPY_GET);
fill_detect(left, right, y+1, fill_color, edge_color, max, count);
}
else if(y == SYS.MAX_Y)
{
fill_process_row(y-1, COPY_GET);
fill_detect(left, right, y-1, fill_color, edge_color, max, count);
}
else
{
fill_process_row(y-1, COPY_GET);
fill_detect(left, right, y-1, fill_color, edge_color, max, count);
fill_process_row(y+1, COPY_GET);
fill_detect(left, right, y+1, fill_color, edge_color, max, count);
}
}

/* 从指定的位置开始填充一定的区域。遇到填充色或边界色停止。 */
/* 出于稳定性的考虑,目前这个版本占用内存很低,速度较慢。 */
int fill(UINT x, UINT y, BITS fill_color, BITS edge_color)
{
int count;
struct point *max = call_fill_stack();

if(max == NULL)
{
return(FAIL);
}
else
{
count = 0;
max[0].x = x;
max[0].y = y;
}
while(count >= 0)
{
fill_row(max, &count, fill_color, edge_color);
}
free(max);
return(GOOD);
}

2006-10-31 15:30
一笔苍穹
Rank: 1
等 级:新手上路
帖 子:640
专家分:0
注 册:2006-5-25
收藏
得分:0 
如果只要想填充一个闭合形状,graphics中有你要的填充函数。
2006-10-31 15:31
whoamiiamye
Rank: 1
等 级:新手上路
帖 子:55
专家分:0
注 册:2006-10-18
收藏
得分:0 

兄弟,谢谢了啊~!我会好好看的。


2006-10-31 16:18
一笔苍穹
Rank: 1
等 级:新手上路
帖 子:640
专家分:0
注 册:2006-5-25
收藏
得分:0 

/*这个使用递归算法的更简单些,思路清晰明了,只要被填充图形不是太大以至堆栈溢出*/

#include "Conio.h"
#include "graphics.h"
#define closegr closegraph

void initgr(void) /* BGI初始化 */
{int gd=DETECT,gm=0; /* 和gd=VGA,gm=VGAHI是同样效果 */
initgraph(&gd,&gm,"C:\\TC");
}
void seedfilling(x,y,fill_color,boundary_color)

int x,y,fill_color,boundary_color;

{

int c;

c=getpixel(x,y); /*获取当前点的颜色*/

if((c!=boundary_color)&&(c!=fill_color)) /*如果颜色为边界色则不填充*/

{

putpixel(x, y, fill_color); /*画点*/
seedfilling(x+1,y, fill_color, boundary_color);

seedfilling(x-1,y, fill_color, boundary_color);

seedfilling(x, y+1, fill_color, boundary_color);

seedfilling(x, y-1, fill_color, boundary_color);

}

}

void main()
{
int a,b,color;

int gd=DETECT , gm;
int poly[10];
a=150 ;
b=140;
color=4;
initgraph(&gd,&gm,"C:\\TC");

poly[0] = 110; /* 第一个点的x坐标以及y坐标 */
poly[1] = 110;
poly[2] = 200; /* 第二点 */
poly[3] = 105;


poly[4] = 170; /* 第三点 */
poly[5] = 120;

poly[6]=150; /*第四点*/
poly[7]=170;

poly[8]=110; /*多边形的起点与终点一样*/
poly[9]=110;
drawpoly(5,poly);/* 显示各点连接起来的多边形 */
seedfilling(a,b,color,15); /*种子填充多边形*/

getch();
closegraph();
}

[此贴子已经被作者于2006-11-1 14:23:30编辑过]

2006-11-01 11:20
whoamiiamye
Rank: 1
等 级:新手上路
帖 子:55
专家分:0
注 册:2006-10-18
收藏
得分:0 

恩,8错,呵呵。


2006-11-01 14:07
快速回复:[求助]求图形填充的代码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015073 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved