/*tu xing xue Full Color */
#include "stdio.h"
#include"graphics.h"
#include"conio.h"
#define POINTNUM 10
#define NULL 0
/*定义点结构体point*/
typedef struct point
{
float x,y;
int style; /*0,1,2 yu book duiying*/
}point; /*dingdian */
/*定义结构体用于活性边表AET*/
typedef struct XET
{
float x, ymax, dx;
struct XET *next;
}AET;/* bian biao */
typedef struct barrel{
float y;
AET *next;
}barrel;/*tong */
/*point Array[POINTNUM]={250,50,0, 550,150,0, 550,400,0, 250,250,0, 100,350,0, 100,100,0 };*/
/*point Array[POINTNUM]={7,8,0, 3,12,0, 1,7,0, 3,1,0, 6,5,0, 8,1,0, 12,9,0, 7,8,0 };*/
point Array[POINTNUM]={70,80,0, 30,120,0, 10,70,0, 30,10,0, 60,50,0, 80,10,0, 120,90,0, 70,80,0 };
int ArrayLength=7;
int ymax,ymin,tongLength;
barrel *tong;
AET *AEThead;
void Paixu();
void Maxy_Miny(int *p,int *q);
void init();
void swap(AET *p1, AET *p2);
void Paixu(AET *p);
void printAET(AET *head,int y);
void Delete(AET *head,int y);
void AlterAET(AET *head);
void Union(AET *head,AET *r);
void Maxy_Miny(int *p,int *q)
{
int i,t; t=(int)(Array[0].y+0.5);
*p=t; *q=t;
for(i=0; i<ArrayLength; i++)
{
t=(int)(Array[i].y+0.5);
if(*p < t) *p=t;
if(*q > t) *q=t;
}
}
int locate(float y)
{
int i;
Maxy_Miny(&ymax,&ymin);
tongLength=ymax-ymin+1;
for(i=0; i<tongLength; i++){
if(tong[i].y==y )
return i;
}
return -1;
}
void init()
{
int i,t,ytemp,weizhi;
AET *p,*q,*r,*AETtemp;
Maxy_Miny(&ymax,&ymin);
tongLength=ymax-ymin+1; printf("/t tongLength= %d /n",tongLength);
tong=(barrel*)malloc(tongLength*sizeof(barrel));
ytemp=ymin;
for(i=0; i<tongLength; i++){
tong[i].y=ytemp; ytemp++;
tong[i].next=NULL;
}
for(i=0; i<ArrayLength; i++)/*给Array[i].style赋值 */
{
t=i-1; if(t<0) t= ArrayLength-1;
if(Array[i].y>=Array[t].y && Array[i].y>=Array[i+1].y)
Array[i].style= 0 ; /*save 0 ge */
else if(Array[i].y<=Array[t].y && Array[i].y<=Array[i+1].y )
Array[i].style= 2 ; /*save 2 ge */
else if( (Array[i].y<=Array[t].y&&Array[i].y>=Array[i+1].y) ||
(Array[i].y>=Array[t].y && Array[i].y<=Array[i+1].y) )
Array[i].style= 1 ; /*save 1 ge */
}
for(i=0; i<ArrayLength; i++)/*ArrayLength=7;*/
{
t=i-1; if(t<0) t= ArrayLength-1;
if(Array[i].y >= Array[t].y && Array[i].y >= Array[i+1].y)
{ continue; /*save 0 ge */ }
else if(Array[i].y <= Array[t].y && Array[i].y <= Array[i+1].y ) /*save 2 ge */
{
p=(AET*)malloc(sizeof(AET)); q=(AET*)malloc(sizeof(AET));
p->x=q->x=Array[i].x;
if(Array[t].style==1) p->ymax=Array[t].y-1; /*?????? Array[t].y-1; */
else p->ymax=Array[t].y;
if(Array[i+1].style==1) q->ymax=Array[i+1].y-1; /*?????? Array[t].y-1; */
else q->ymax=Array[i+1].y;
if( (Array[i].y-Array[t].y) !=0)
p->dx=(Array[i].x-Array[t].x)/(Array[i].y-Array[t].y);
if( (Array[i].y-Array[i+1].y) !=0)
q->dx=(Array[i].x-Array[i+1].x)/(Array[i].y-Array[i+1].y);
weizhi=locate(Array[i].y);
AETtemp=tong[weizhi].next;
tong[weizhi].next=p;
p->next=q;
q->next=AETtemp;
}
else if( (Array[i].y <= Array[t].y&&Array[i].y >= Array[i+1].y) ||
(Array[i].y >= Array[t].y && Array[i].y <= Array[i+1].y) )/*save1ge*/
{
r=(AET*)malloc(sizeof(AET));
r->x=Array[i].x;
if(Array[t].y > Array[i+1].y)
{
if(Array[t].style==1) r->ymax=Array[t].y-1; /*?????? Array[t].y-1; */
else r->ymax=Array[t].y;
if((Array[i].y-Array[t].y)!=0)
r->dx=(Array[i].x-Array[t].x)/(Array[i].y-Array[t].y);
}else
{
if(Array[i+1].style==1) r->ymax=Array[i+1].y-1; /*?????? Array[t].y-1; */
else r->ymax=Array[i+1].y;
if((Array[i].y-Array[i+1].y)!=0)
r->dx=(Array[i].x-Array[i+1].x)/(Array[i].y-Array[i+1].y);
}
weizhi=locate(Array[i].y);
AETtemp=tong[weizhi].next;
tong[weizhi].next=r;
r->next=AETtemp;
}
}
for(i=0; i<tongLength; i++)
if(tong[i].next!=NULL)
Paixu(tong[i].next);
}
void swap(AET *p1, AET *p2)
{ float itemp;
itemp = p1->x;
p1->x = p2->x;
p2->x = itemp;
itemp = p1->ymax;
p1->ymax = p2->ymax;
p2->ymax = itemp;
itemp = p1->dx;
p1->dx = p2->dx;
p2->dx = itemp;
}
void Paixu(AET *head)/*head不为空*/
{
AET *p,*q; int i;
for(i=1; i<10; i++)
{
p=head; q=p->next;
while(q!=NULL)
{
if(p->x > q->x)
swap(p,q);
else if(p->x == q->x)
if(p->dx >= q->dx)
swap(p,q);
p=p->next; q=p->next;
}
}
}
void printAET(AET *head,int y)
{ AET *p=head->next,*q;
int b=1; float x1,x2; int ix;
int midx,midy;
midx=(1+getmaxx())/2;
midy=(1+getmaxy())/2;
while(p->next!=NULL)
{
if(b==1)
{
q=p->next;
x1=p->x; x1=(int)(x1+0.5);
x2=q->x; x2=(int)(x2+0.5);
for(ix=x1; ix<=x2; ix++)
{ /*putpixel(0+ix,0+y,3);*/
putpixel(0+ix,2*midy-y,3);
}
/*putpixel(midx+ix,midy+y,3);printf("(%d, %d) /t",ix,y); */
b=0;
}
else if(b==0) b=1;
p=p->next;
}
}
void Delete(AET *head,int y)
{
AET *pre=head,*p=pre->next;
int ytemp;
while(p!=NULL)
{
ytemp=p->ymax;
if(ytemp==y)
{ pre->next=p->next;
p=pre->next;
}
else
{
pre=pre->next;
p=pre->next;
}
}
}
void AlterAET(AET *head)
{ AET *p=head->next;
while(p!=NULL)
{
p->x = p->x + p->dx;
p=p->next;
}
}
void Union(AET *head,AET *r)
{ AET *p=head->next;
while(p->next !=NULL)
p=p->next;
p->next=r;
}
int numtong(int y)
{ int i;
for(i=0; i<tongLength; i++){
if(tong[i].y==y)
return i;
}
return -1;
}
void inputpoint()
{ int i;
float fx;
printf("/nyou will input how many points(num <10) ?/n");
scanf("%f",&fx); ArrayLength=fx;
for(i=0; i<ArrayLength; i++)
{
printf (" Input di %d ge (x y): ",i) ;
scanf("%f",&Array[i].x);
scanf("%f",&Array[i].y);
}
}
void progress()
{
int gdriver=DETECT,gmode;
int y;
AEThead=(AET*)malloc(sizeof(AET));
initgraph(&gdriver,&gmode,"c://tc");
AEThead->next=tong[0].next;
for(y=ymin; y<=ymax; )
{
printAET(AEThead,y); /* printf("/n"); */
Delete(AEThead,y);
y++; AlterAET(AEThead);
if(tong[numtong(y)].next!=NULL && numtong(y)!=-1)
{
Union(AEThead,tong[numtong(y)].next);
Paixu(AEThead);
}
if(AEThead->next==NULL)
break;
}
getch();
closegraph();
}
main()
{
/* inputpoint();*/
init();
progress();
return(0);
}