📄 cpp1.cpp
字号:
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<malloc.h>
int mx,my;
/******************************************************/
struct Point *getPoint(void);
struct Pail *makePail(struct Point *n);
void fillGraph(struct Pail *n,int color);
void insertVex(struct Pail *a,struct Vex *b,int c);
void insertAET(struct Vex *vex,struct Vex *v);
void putPoint(int x,int y,int color);
struct Vex *sort(struct Vex *t);
/******************************************************/
struct Point{
int x;
int y;
struct Point *next;
};
struct Pail{
int y;
struct Vex *vex;
struct Pail *next;
};
struct Vex{
float x;
int maxy;
float k;
struct Vex *next;
};
/******************************************************/
void main()
{ int gd,gm;
struct Point *point;
struct Pail *pail;
struct Pail *test;
struct Vex *vex;
gd=DETECT;
point=getPoint();
pail=makePail(point);
printf("%d",pail->y);
test=pail;
while(test->next!=NULL)
{ test=test->next;
printf("\\n\\n%d\\n",test->y);
vex=test->vex;
while(vex!=NULL)
{ printf("%f,%d,%f\\n",vex->x,vex->maxy,vex->k);
vex=vex->next;
}
}
getch();
initgraph(&gd,&gm,"d:\\\\tc");
mx=getmaxx();my=getmaxy();
line(mx/2,0,mx/2,my);
line(0,my/2,mx,my/2);
fillGraph(pail,9);
getch();
closegraph();
}
struct Point *getPoint()
{ struct Point *head,*p,*q;
int x,y,x0,y0,first;
p=head=(struct Point *)malloc(sizeof(struct Point));
printf("input x,y:");
scanf("%d,%d",&x0,&y0);
x=x0;y=y0;first=1;
head->x=x;head->y=y;
while((x!=x0||y!=y0)||first==1)
{ first=0;
printf("input x,y:");
scanf("%d,%d",&x,&y);
q=(struct Point *)malloc(sizeof(struct Point));
q->x=x;q->y=y;
p->next=q;
p=p->next;
}
p->next=NULL;
return head;
}
struct Pail *makePail(struct Point *pointhead)
{ struct Point *pt;
struct Vex *lvex,*vex;
struct Pail *pailhead;
int x0,y0,x1,y1,first,min_y,lmin_y;
float k;
first=1;
lvex=NULL;
lmin_y=0;
pailhead=(struct Pail *)malloc(sizeof(struct Pail));
pailhead->y=0; /*the y of head store the num of edge table;*/
pailhead->next=NULL;
pt=pointhead;
while(pt->next!=NULL)
{ x0=pt->x;y0=pt->y;
x1=pt->next->x;y1=pt->next->y;
if(y0!=y1)
{ k=(float)(x1-x0)/(float)(y1-y0);
vex=(struct Vex *)malloc(sizeof(struct Vex));
vex->x=(y0<y1?x0:x1);
vex->maxy=(y0<y1?y1:y0);
vex->k=k;
vex->next=NULL;
min_y=(y0<y1?y0:y1);
if(!first) /*decrease the maxy*/
{ if(min_y==lvex->maxy)lvex->maxy--;
else if(lmin_y==vex->maxy)vex->maxy--;
}
lvex=vex;lmin_y=min_y;first=0;
insertVex(pailhead,vex,min_y);
}
pt=pt->next;
}
return pailhead;
}
void insertVex(struct Pail *pailhead,struct Vex *vex,int min_y)
{ struct Pail *tpail,*pail;
struct Vex *svex,*tvex;
pail=pailhead;
while((pail->next!=NULL)&&(pail->next->y<min_y))pail=pail->next;
if(pail->next==NULL||pail->next->y!=min_y)
{ pailhead->y++;
tpail=(struct Pail *)malloc(sizeof(struct Pail));
tpail->y=min_y;
tpail->vex=NULL;
tpail->next=pail->next;
pail->next=tpail;
}
pail=pail->next;
if(pail->vex==NULL)pail->vex=vex;
else
{ tvex=pail->vex;
svex=tvex->next;
while((tvex->next!=NULL)&&(tvex->next->x<vex->x))
{ tvex=svex;
svex=svex->next;
}
while((tvex->next!=NULL)&&(svex->x==vex->x)&&(svex->k<vex->k))
{ tvex=svex;
svex=svex->next;
}
vex->next=svex;
tvex->next=vex;
}
}
void fillGraph(struct Pail *headpail,int color)
{ struct Vex *AET,*tvex,*svex,*ivex,*jvex;
struct Pail *tpail;
int x,i,j,flag;
tpail=headpail->next;
AET=(struct Vex *)malloc(sizeof(struct Vex));
AET->x=0;
AET->maxy=0;
AET->k=0;
AET->next=NULL;
if(headpail->next!=NULL)
{ AET->next=tpail->vex;
AET->maxy=tpail->y;
}
while(AET->next!=NULL)
{
svex=AET;
tvex=svex->next;flag=0;
while(svex->next!=NULL) /*delete the node*/
{ if((svex->next!=NULL)&&(tvex->maxy==AET->maxy))
{ svex->next=tvex->next;
printf("Delect:%d,%f\\n",tvex->maxy,tvex->k);
tvex=svex->next;
flag=1;
}
if(flag==0)
{ svex=tvex;
tvex=tvex->next;
}
if(flag==1)flag=0;
}
tvex=AET->next;flag=0;
while(tvex->next!=NULL)
{ if(!flag)for(x=(int)tvex->x;x<=(int)tvex->next->x;x++)
putPoint(x,AET->maxy,color);
tvex=tvex->next;
if(flag==0)flag=1;
else flag=0;
}
svex=AET;
tvex=svex->next;flag=0;
while(svex->next!=NULL) /*delete the node*/
{ if((svex->next!=NULL)&&(tvex->maxy==AET->maxy))
{ svex->next=tvex->next;
printf("Delect:%d,%f\\n",tvex->maxy,tvex->k);
tvex=svex->next;
flag=1;
}
if(flag==0)
{ svex=tvex;
tvex=tvex->next;
}
if(flag==1)flag=0;
}
tvex=AET;
while(tvex->next!=NULL) /*increase the x*/
{tvex=tvex->next;
tvex->x=tvex->x+tvex->k;
}
AET->maxy++;
if((tpail->next!=NULL)&&(AET->maxy==tpail->next->y))
{ printf("\\n\\nInserting...\\n");
ivex=tpail->next->vex;
while(ivex!=NULL)
{ jvex=ivex;
ivex=ivex->next;
insertAET(AET,jvex);
}
tpail=tpail->next;
}
sort(AET);
}
}
void insertAET(struct Vex *AET,struct Vex *pailvex)
{
struct Vex *vex;
vex=AET;
while((vex->next!=NULL)&&(pailvex->x>vex->next->x))vex=vex->next;
while((vex->next!=NULL)&&(pailvex->x==vex->next->x)&&
(pailvex->k>vex->next->k))vex=vex->next;
printf("Insert:%f,%d\\n",pailvex->x,pailvex->maxy);
pailvex->next=vex->next;
vex->next=pailvex;
vex=AET;
while(vex->next!=NULL)
{ vex=vex->next;
printf("Now:%f,%d,%f\\n",vex->x,vex->maxy,vex->k);
}
}
struct Vex *sort(struct Vex *AET)
{ struct Vex *vex,*vexhead,*midvex,*tvex;
vexhead=(struct Vex *)malloc(sizeof(struct Vex));
vexhead->maxy=AET->maxy;
vexhead->next=NULL;
tvex=vexhead;
vex=AET->next;
while(vex!=NULL)
{ midvex=vex;
vex=vex->next;
while((tvex->next!=NULL)&&(midvex->x>tvex->next->x))tvex=tvex->next;
midvex->next=tvex->next;
tvex->next=midvex;
}
return vexhead;
}
void putPoint(int x,int y,int color)
{ delay(1000);
putpixel(x+mx/2,-y+my/2,color);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -