⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ael_show.c

📁 计算机图形学
💻 C
字号:
/*本方法优点是:原始图形脏时仍然填充原图 */

#include "stdio.h"
#include"stdlib.h"
#include "math.h"
#define pi 3.14159265
#include "Conio.h"
#include "graphics.h"
#include "malloc.h"
#define closegr closegraph
#define max 400

typedef struct Edge{
int ymax;
float x,deltax;
struct Edge *nextEdge;
}Edge;

typedef struct {
 char *s,*t,*w,*v,*v1;
 }string;

Edge *ET[max];

void initgr(void) /* BGI初始化 */
{int gd=DETECT,gm=0; /* 和gd=VGA,gm=VGAHI是同样效果 */
 registerbgidriver(EGAVGA_driver);/* 注册BGI后可以驱动不需要.BGI文件的支持运行 */
 initgraph(&gd,&gm,"");
}
void GetST(float x[5],float y[5],float a[5],float b[5],float radius)
{int i;
float r;
setcolor(BLUE);
 for(i=0;i<5;i++)
 {x[i]=radius*cos(i*0.4*pi+0.05*pi)-radius*sin(i*0.4*pi+0.05*pi)+450;
  y[i]=radius*sin(i*0.4*pi+0.05*pi)+radius*cos(i*0.4*pi+0.05*pi)+200;
  getch();
 circle(x[i],y[i],1); fillellipse(x[i],y[i],1,1);

 }
 r=sin(0.1*pi)*radius/sin(126.0/180.0*pi);
  for(i=0;i<5;i++)
 {a[i]=r*cos(i*0.4*pi+0.25*pi)-r*sin(i*0.4*pi+0.25*pi)+450;
  b[i]=r*sin(i*0.4*pi+0.25*pi)+r*cos(i*0.4*pi+0.25*pi)+200;
 getch();
 circle(a[i],b[i],1);

  }
  /*得到五角星的10个顶点*/
  }

 void Del_Node(int left,int top,int right,int bottom)
 {int a[8];
 a[0]=left;a[1]=top;a[2]=left;a[3]=bottom;a[4]=right;a[5]=bottom;a[6]=right;a[7]=top;
   setcolor(WHITE);
   fillpoly(4,a);
 }

  void Node_Rect(int left,int top,int right,int bottom,string a,int flag)
{int m;
 rectangle(left,top,right,bottom);
      for(m=1;m<4;m++) line(left+m*30,top,left+m*30,bottom);
      if(flag==0){outtextxy(left+2,top+7,a.w); outtextxy(left+30+2,top+7,a.v); outtextxy(left+60+5,top+2,a.s);outtextxy(left+60+7,top+4,"_");
    outtextxy(left+60+5,top+15,a.t); }
    else {outtextxy(left+2,top+7,a.w); outtextxy(left+30+2,top+7,a.v1); outtextxy(left+60+2,top+4,"-");
     outtextxy(left+60+10,top+2,a.s);outtextxy(left+60+11,top+4,"_");outtextxy(left+60+10,top+15,a.t);}
}

void CreatET(float x[5],float y[5],float a[5],float b[5])
 {int n;
 Edge *e,*h;
 n=(int)(y[3]+0.5);
 e=(Edge*)malloc(sizeof(Edge));
 e->ymax=(int)(b[3]+0.5);e->x=x[3];e->deltax=(a[3]-x[3])/(b[3]-y[3]);
 h=(Edge*)malloc(sizeof(Edge));
 h->ymax=(int)(b[2]+0.5);h->x=x[3];h->deltax=(a[2]-x[3])/(b[3]-y[3]);
 e->nextEdge=h;h->nextEdge=NULL;
 ET[n]=e;


 n=(int)(b[2]+0.5);
 e=(Edge*)malloc(sizeof(Edge));
 e->ymax=(int)(b[1]+0.5);e->x=x[2];e->deltax=(a[1]-x[2])/(b[1]-y[2]);
 h=(Edge*)malloc(sizeof(Edge));
 h->ymax=(int)(b[4]+0.5);h->x=x[4];h->deltax=(a[4]-x[4])/(b[4]-y[4]);
 e->nextEdge=h;h->nextEdge=NULL;
 ET[n]=e;


  n=(int)(b[1]+0.5);
e=(Edge*)malloc(sizeof(Edge));
e->ymax=(int)(y[1]+0.5);e->x=a[1];e->deltax=(x[1]-a[1])/(y[1]-b[1]);
 h=(Edge*)malloc(sizeof(Edge));
 h->ymax=(int)(y[0]+0.5);h->x=a[4];h->deltax=(x[0]-a[4])/(y[0]-b[4]);
 e->nextEdge=h;h->nextEdge=NULL;
 ET[n]=e;


  n=(int)(b[0]+0.5);
 e=(Edge*)malloc(sizeof(Edge));
 e->ymax=(int)(y[1]+0.5);e->x=a[0];e->deltax=(x[1]-a[0])/(y[1]-b[0]);
 h=(Edge*)malloc(sizeof(Edge));
 h->ymax=(int)(y[0]+0.5);h->x=a[0];h->deltax=(x[0]-a[0])/(y[0]-b[0]);
 e->nextEdge=h;h->nextEdge=NULL;
 ET[n]=e;


 }

 void Free(float y[5],float b[5])
 {int n;
 n=(int)(y[3]+0.5);
 ET[n]=NULL;
 n=(int)(b[2]+0.5);
 ET[n]=NULL;
 n=(int)(b[1]+0.5);
 ET[n]=NULL;
 n=(int)(b[0]+0.5);
 ET[n]=NULL;
}
void Arrow_Line(int leftx,int lefty,int rightx,int righty)
{line(leftx,lefty,rightx,righty);
 line(rightx-5,righty+5,rightx,righty);line(rightx-5,righty-5,rightx,righty);
}
 void Fillin(Edge *AEL,int bottom,int top )
 {
 char *s;
 string str;
 Edge *e;
  Edge *p,*q;
  int n,m,i=0,j=0,a[8];
 AEL=(Edge*)malloc(sizeof(Edge));AEL->x=-1;
AEL->nextEdge=NULL;
for(n=bottom;n<top+1;n++)
{p=ET[n];
 while(p)
 {  getch();
    setcolor(BLUE);
    switch(n)
      {case 59:str.s="31";str.t="97";str.w="146";str.v="200";str.v1="200";break;

       case 156:str.s="83";str.t="60";str.w="207";str.v="66";str.v1="334";break;
       case 217:str.s="31";str.t="97";str.w="314";str.v="148";str.v1="251";break;
       case 254:str.s="83";str.t="60";str.w="314";str.v="200";str.v1="200";break;
      default:break;
      }


   if(i%2==0)
   { if(n==59||n==217){
       if(n==217)  {setcolor(WHITE); line(60,390,70,390);line(70,390,70,410);line(70,410,50,410);line(50,410,50,430);
      Arrow_Line(50,430,60,430);}
      setcolor(BLUE);
   Arrow_Line(60,390,150,390);
   Node_Rect(150,380,250,405,str,0);
    Arrow_Line(245,392,420,392); }
     if(n==156){ setcolor(WHITE) ;Arrow_Line(60,390,150,390);
     setcolor(BLUE);line(60,390,70,390);line(70,390,70,410);line(70,410,50,410);line(50,410,50,430);
      Arrow_Line(50,430,60,430);
     Node_Rect(60,420,160,445,str,0);Arrow_Line(155,430,490,430); }
     if(n==254){setcolor(WHITE);Arrow_Line(245,392,420,392); 
     setcolor(BLUE);
     line(245,390,260,390);line(260,390,260,410);line(260,410,210,410);line(210,410,210,430);
     Arrow_Line(210,430,220,430);
      Node_Rect(220,420,320,445,str,0); Arrow_Line(315,430,330,430);


     }

   Del_Node(10,j*80+70,119,j*80+95);}

   else if(i%2!=0)
   { if(n==59)Node_Rect(420,380,520,405,str,1);
     if(n==156) Node_Rect(490,420,590,445,str,1);
     if(n==217) {Node_Rect(420,380,520,405,str,1);line(515,390,530,390);line(530,390,530,410);
     line(530,410,480,410);line(480,410,480,430);
      Arrow_Line(480,430,490,430);}
     if(n==254) {Node_Rect(330,420,430,445,str,1);
     line(425,430,440,430);line(440,430,440,410);line(440,410,410,410);line(410,410,410,390);
    Arrow_Line(410,390,420,390); }
 Del_Node(120,j*80+70,220,j*80+95);
    j+=1;}


   i+=1;
e=AEL; q=AEL->nextEdge;
while(q)
  {if(p->x<q->x)break;
   q=q->nextEdge;
   e=e->nextEdge;}
   q=p->nextEdge;
   p->nextEdge=e->nextEdge;
   e->nextEdge=p;
   p=q;


}

p=AEL;q=AEL->nextEdge;
m=1;
getch();
 while(q)
 { if(q->ymax==n)
   { /*outtextxy(2,i+10,"ScanLine have reached");
     i+=10;
     outtextxy(2,i+10,"one elem'ymax of AEL;");
     i+=10;
     outtextxy(2,i+10,"remove it from AEL;"); i+=20; */
    setcolor(WHITE);
if(n==156){Del_Node(420,380,520,405); getch();Arrow_Line(245,392,420,392);Del_Node(150,380,250,405); }
if(n==217) {Del_Node(490,420,590,445);line(515,390,530,390);line(530,390,530,410);
             line(530,410,480,410);line(480,410,480,430);
             Arrow_Line(480,430,490,430); getch();Arrow_Line(155,430,490,430); Del_Node(60,420,160,445);}
if(n==314) {Del_Node(420,380,520,405);line(425,430,440,430);line(440,430,440,410);line(440,410,410,410);
         line(410,410,410,390);Arrow_Line(410,390,420,390);
        getch();Del_Node(330,420,430,445);Arrow_Line(315,430,330,430);getch();
         Del_Node(220,420,320,445); line(245,390,260,390);line(260,390,260,410);line(260,410,210,410);line(210,410,210,430);
        Arrow_Line(210,430,220,430); getch();
        Del_Node(150,380,250,405);  Arrow_Line(60,390,150,390);}
i-=1;

 p->nextEdge=q->nextEdge;
   e=q;
   q=q->nextEdge;free(e);
   }
 else{
   if(p!=AEL&&m%2==0)
   {setcolor(RED);
   line((int)(p->x+0.5),n,(int)(q->x+0.5),n);
    p->x+=p->deltax;
    q->x+=q->deltax;}
    p=p->nextEdge;
    q=q->nextEdge;
    m++;}
 }
 }
 }


main()
{ char *s,*t,*w,*v,*v1;
  string str;
 int n,m ,i;
 float x[5],y[5],a[5],b[5],r;
  Edge *AEL;
initgr();/* BGI初始化 */
setbkcolor(WHITE);
setcolor(BLUE);
outtextxy(1,1,"Show Pentagram");

GetST(x,y,a,b,100); /*得到五角星的顶点*/
setcolor(9);
for(i=0;i<4;i++)
  {getch();
  line(x[i],y[i],a[i],b[i]);getch();
   line(a[i],b[i],x[i+1],y[i+1]);
}
getch();
  line(x[4],y[4],a[4],b[4]); getch();
  line(a[4],b[4],x[0],y[0]);
  /*画出五角星*/
 getch();
 line(x[3]-170,10,x[3]+170,10);
 line(x[3]+170-5,5,x[3]+170,10);
 line(x[3]+170-5,15,x[3]+170,10);
 line(x[3]-170,10,x[3]-170,360);
 line(x[3]-170-5,355,x[3]-170,360);
 line(x[3]-170+5,355,x[3]-170,360);
 outtextxy(x[3]-170-5,5,"0"); /*画坐标*/
 for(i=10;i<350;i+=10)
 {if(i%50==0)
   {
  line(x[3]-170+i,10,x[3]-170+i,20);
  if(i==50) s=ecvt(i,2,1,1);
   else s=ecvt(i,3,1,1);
    outtextxy(x[3]-170-25,i-5,s);}

  else
 line(x[3]-170+i,10,x[3]-170+i,15);


}

 for(i=10;i<350;i+=10)
 {if(i%50==0)
  {line(x[3]-170,i,x[3]-170+15,i);
   if(i==50) s=ecvt(i,2,1,1);
   else s=ecvt(i,3,1,1);
    outtextxy(x[3]-170+i-10,2,s);
  }
  else
 line(x[3]-170,i,x[3]-170+5,i);
 }

 /*标坐标轴刻度*/
  setcolor(RED);
  getch();
   outtextxy(x[3]-170-20,58,"49"); getch();
line(x[3]-170,58,x[3]+100,58); outtextxy(x[3]+102,58,"ET[49]"); getch();
  outtextxy(x[3]-170-25,156,"146"); getch();
 line(x[3]-170,156,x[3]+150,156); outtextxy(x[3]+132,148,"ET[146]"); getch();
  outtextxy(x[3]-170-25,217,"207"); getch();
 line(x[3]-170,217,x[3]+100,217);  outtextxy(x[3]+102,217,"ET[207]"); getch();
  outtextxy(x[3]-170-25,254,"244"); getch();
 line(x[3]-170,254,x[3]+120,254);   outtextxy(x[3]+122,254,"ET[244]");  getch();
   /*划分类边*/
   getch();
   outtextxy(10,20,"Edge Table:"); getch();
for(i=0;i<4;i++)
    { rectangle(10,40+i*80,80,i*80+60);
       line(70,i*80+50,90,i*80+50);line(90,i*80+50,90,i*80+65); line(90,i*80+65,20,i*80+65);
       line(20,i*80+65,20,i*80+70); line(18,i*80+68,20,i*80+70);line(22,i*80+68,20,i*80+70);
 }
 outtextxy(15,45,"ET[49]");  outtextxy(15,125,"ET[146]");
    outtextxy(15,205,"ET[207]"); outtextxy(15,285,"ET[244]");
for(i=0;i<4;i++)
 {switch(i)
      {case 0:str.s="31";str.t="97";str.w="146";str.v="200";str.v1="200";break;
       case 1:str.s="83";str.t="60";str.w="207";str.v="66";str.v1="334";break;
       case 2:str.s="31";str.t="97";str.w="314";str.v="148";str.v1="251";break;
       case 3:str.s="83";str.t="60";str.w="314";str.v="200";str.v1="200";break;
      default:break;
      }


 Node_Rect(10,i*80+70,110,i*80+95,str,0);
 line(105,i*80+85,120,i*80+85);line(118,i*80+83,120,i*80+85);line(118,i*80+87,120,i*80+85);
 Node_Rect(120,i*80+70,220,i*80+95,str,1);
 }

 /*画分类表*/

 n=(int)(y[3]+0.5);
  m=(int)(y[1]+0.5);
   getch();
   CreatET(x,y,a,b);
   setcolor(BLUE); outtextxy(10,360,"Active Edge List:");
   rectangle(20,380,60,400); outtextxy(25,385,"AEL");
   Fillin(AEL,n,m); /*填充*/
outtextxy(200,400,"Fillin Finished!");





 getch();

 /* 暂停一下,看看前面绘图代码的运行结果 */
 closegr(); /* 恢复TEXT屏幕模式 */
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -