扫描线区域填充算法

 

 

 
 typedef struct{
int y_top;
float x_int;
int delta_y;
float x_change_per_scan;
}EACH_ENTRY;

EACH_ENTRY sides[MAX_POINT];
int x[MAX_POINT],y[MAX_POINT];
int side_count,first_s,last_s,scan,bottomscan,x_int_count;

fill_area(count,x,y)
int count,x[],y[];
{
sort_on_bigger_y(count);
first_s=1;
last_s=1;
for(scan=sides[1].y_top;scan>bottomscan;scan--)
{
update_first_and_last(count,scan);
process_x_intersections(scan,first_s,last_s);
draw_lines(scan,x_int_count,first_s);
update_sides_list();
}
}

void put_in_sides_list(entry,x1,y1,x2,y2,next_y)
int entry,x1,x2,y1,y2,next_y;
{
int maxy;
float x2_temp,x_change_temp;
x_change_temp=(float)(x2-x1)/(float)(y2-y1);
x2_temp=x2; /*消除左右顶点*/
if((y2>y1)&&(y2<next_y)){
y2--;
x2_temp-=x_change_temp;
}
else{
if((y2<y1)&&(y2>next_y)){
y2++;
x2_temp+=x_change_temp;
}
}
/*以下为插入活性边表操作*/
maxy=(y1>y2)?y1:y2;
while((entry>1)&&(maxy>sides[entry-1].y_top))
{
sides[entry]=sides[entry-1];
entry--;
}
sides[entry].y_top=maxy;
sides[entry].delta_y=abs(y2-y1)+1;
if(y1>y2)
sides[entry].x_int=x1;
else
sides[entry].x_int=x2_temp;
sides[entry].x_change_per_scan=x_change_temp;
}

void sort_on_bigger_y(n)
int n;
{
int k,x1,y1;
side_count=0;
y1=y[n];
x1=x[n];
bottomscan=y[n];
for(k=1;k<n+1;k++)
{
if(y1!=y[k]){
side_count++;
put_in_sides_list(side_count,x1,y1,x[k],y[k],y[k+1]);
}
else{
move((short)x1,(short)y1);
line((shortx[k],(short)y1,status);
}
if(y[k]<bottomscan) bottomscan=y[k];
y1=y[k];x1=x[k];
}
}

void update_first_and_last(count,scan)
int count,scan;
{
while((sides[last_s+1].y_top>=scan)&&(last_s<count))
last_++;
while(sides[first_s].delta_y==0) first_s++;
}

void swap(x,y)
EACH_ENTRY x,y;
{
int i_temp;
float f_temp;
i_temp=x.y_top;x.y_top=y.y_top;y.y_top=i_temp;
f_temp=x.x_int;x.x_int=y.x_int;y.x_int=f_temp;
i_temp=x.delta_y;x.delta_y=y.delta_y;y.delta_y=i_temp;
f_temp=x.x_change_per_scan;
x.x_change_per_scan=y.x_change_per_scan;
y.x.change_per_scan=f_temp;
}

void sort_on_x(entry,first_s)
int entry,first_s;
{
while((entry>first_s)&&(sides[entry].x_int<sides[entry].x_int))
{
swap(sides[entry],sides[entry-1]);
entry--;
}
}

void process_x_intersections(scan,first_s,last_s)
int scan,first_s,last_s;
{
int k;
x_int_count=0;
for(k=first_s;k<last_s+1;k++)
{
if(sides[k].delta_y>0){
x_int_count++;
sort_on_x(k,first_s);
}
}
}

void draw_lines(scan,x_int_count,index)
int scan,x_int_count,index;
{
int k,x,x1,x2;
for(k=1;k<(int)(x_int_count/2+1.5);k++)
{
while(sides[index].delta_y==0) index++;
x1=(int)(sides[index].x_int+0.5);
index++;
while(sides[index].delta_y==0) index++;
x2=(int)(sides[index].x_int+0.5);
move((short)x1,(short)scan);
line((short)x2,(short)scan,status);
index++;
}
}

void update_sides_list()
{
int k;
for(k=first_s;k<last_s+1;k++)
{
if(sides[k].deltay>0)
{
sides[k].delta_y--;
sides[k].x_int-=sides[k].x_change_per_scan;
}
}
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值