www.pudn.com > 02824619.rar > ael.cpp
#include#include #include #include #include #include #define SCREEN_WIDTH 500 //----------------------------------- //Here, please define your own data structure for edge Table, Active Edge Table, Edge //----------------------------------- typedef struct tEdge //边的结构体 { int yUpper; //边的最大Y值 float xIntersect,dxPerScan; //扫描线与边的交点的X值,边斜率的倒数 struct tEdge *next; }Edge; typedef struct tdcPt //点的结构体 { int x; int y; }dcPt; dcPt pts[15]; //存放顶点的数组 void insertEdge(Edge *list,Edge *edge) //插入边函数 ,按交点X的递增排序 { Edge *p,*q=list; p=q->next; while(p!=NULL){ //判断后续是否空 if(edge->xIntersect < p->xIntersect) //判断后续与插入边分别交点的X值, p=NULL; //直到后者比前者小 else{ q=p; p=p->next; } } edge->next=q->next; //实现插入 q->next=edge; } int yNext(int k,int cnt,dcPt *pts) //索引,返回下一条非平行的纵座标值 { int j; if((k+1)>(cnt-1)) j=0; else j=k+1; while(pts[k].y==pts[j].y) if((j+1)>(cnt-1)) j=0; else j++; return(pts[j].y); } void makeEdgeRec(dcPt lower,dcPt upper, int yComp,Edge *edge,Edge *edges[]) //创建多边形的边 { edge->dxPerScan=(float)(upper.x-lower.x)/(upper.y-lower.y); //dx edge->xIntersect=(float)lower.x; //交点的x值 if(upper.y yUpper=upper.y-1; else edge->yUpper=upper.y; insertEdge(edges[lower.y],edge); } void buildEdgeList(int cnt,dcPt *pts,Edge *edges[]) // 创建边表 { Edge *edge; dcPt v1,v2; int i,yPrev=pts[cnt-2].y; v1.x=pts[cnt-1].x; v1.y=pts[cnt-1].y; for(i=0;i next; while(p){ q=p->next; insertEdge(active,p); // 插入 p=q; } } void deleteAfter (Edge *q) //删除边 { Edge *p=q->next; q->next=p->next; delete p; } void updateActiveList(int scan,Edge *active) // 更新活化边表 { Edge *q=active,*p=active->next; while(p) //直到P空,即遍历活化边表 if(scan>=p->yUpper){ p=p->next; deleteAfter(q); } else{ p->xIntersect=p->xIntersect+p->dxPerScan; q=p; p=p->next; } } void resortActiveList(Edge *active) //应用活化边表 { Edge *q,*p=active->next; active->next=NULL; while(p){ q=p->next; insertEdge(active,p); p=q; } } void DrawPoint(int x, int y) //给定的画点函数 { glBegin(GL_POINTS); glVertex2d(x, y); glEnd(); } void fillScan(int scan,Edge *active) //扫描填充 { Edge *p1,*p2; float i; p1=active->next; while(p1){ p2=p1->next; for(i=p1->xIntersect;i xIntersect;i++) { ::glColor3f(1.0, 0.0, 0.0); DrawPoint((int)i,scan); } p1=p2->next; } } void scanFill(int cnt,dcPt *pts) { Edge *edges[SCREEN_WIDTH],*active; int i,scan; for(i=0;i next=NULL; } buildEdgeList(cnt,pts,edges); active=new Edge; active->next=NULL; for(scan=0;scan next) { fillScan(scan,active); updateActiveList(scan,active); resortActiveList(active); } } } void CALLBACK Reshape(GLsizei w, GLsizei h) //给定的 { ::glViewport(0, 0, w, h); ::glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w <= h) gluOrtho2D (0.0, SCREEN_WIDTH, 0.0, SCREEN_WIDTH*(GLfloat)h/(GLfloat)w); else gluOrtho2D (0.0, (GLfloat)SCREEN_WIDTH*(GLfloat)w/(GLfloat)h, 0.0, SCREEN_WIDTH); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); return; } int count; void CALLBACK Display() //给定的 { ::glColor3f(1.0, 1.0, 0.0); //----------------------------------- // use the function "DrawPoint"defined above to display // your scan conversion result here //----------------------------------- glPointSize(5); glClear(GL_COLOR_BUFFER_BIT); scanFill(count,pts); glFlush(); auxSwapBuffers(); return; } void Init() //给定的初始化窗口 { ::auxInitDisplayMode(AUX_RGBA|AUX_SINGLE); ::auxInitPosition(0, 0, 500, 500); ::auxInitWindow("Scan algithmic"); ::glShadeModel(GL_FLAT); ::glClearColor(0.0, 0.0, 0.0, 0.0); } void main(int argc, char **argv) { Init(); //----------------------------------- // implement your scan conversion algorithm here! //----------------------------------- cout<<"请输入要测试的多边形的顶点个数"< >count; for(int i=0;i >pts[i].x>>pts[i].y; } ::auxReshapeFunc(Reshape); ::auxMainLoop(Display); return ; }