资源描述:
《扫描线算法代码.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、#include#include#include"stdlib.h"voidinit(void){glClearColor(1.0,1.0,1.0,0.0);//指定清空颜色(背景色)为白色glMatrixMode(GL_PROJECTION);//指定投影矩阵gluOrtho2D(0.0,400.0,0.0,400.0);//指定二维坐标系中被显示的区域}typedefstructtEdge{intyUpper;floatxIntersect,dxPerScan;structtE
2、dge*next;}Edge;structdcPt{//dcPt实际上是一个点的结构体intx;inty;};voidsetPixel(GLintx,GLinty){glBegin(GL_POINTS);glVertex2i(x,y);glEnd();}/*InsertsedgeintolistinorderofincreasingxIntersectfield.*/voidinsertEdge(Edge*list,Edge*edge){Edge*p,*q=list;p=q->next;while(p!=NULL){if
3、(edge->xIntersectxIntersect)p=NULL;else{q=p;p=p->next;}}edge->next=q->next;q->next=edge;}/*Foranindex,returny-coordinateofnextnonhorizontalline*/intyNext(intk,intcnt,dcPt*pts){intj;if((k+1)>(cnt-1))j=0;elsej=k+1;while(pts[k].y==pts[j].y)if((j+1)>(cnt-1))j=0;e
4、lsej++;return(pts[j].y);}/*Storelower-ycoordinateandinverseslopeforeachedge.Adjustandstoreupper-ycoordinateforedgesthatarethelowermemberofamonoticallyincreasingordecreasingpairofedges*/voidmakeEdgeRec(dcPtlower,dcPtupper,intyComp,Edge*edge,Edge*edges[]){edge->dxP
5、erScan=(float)(upper.x-lower.x)/(upper.y-lower.y);edge->xIntersect=lower.x;if(upper.yyUpper=upper.y-1;elseedge->yUpper=upper.y;insertEdge(edges[lower.y],edge);}voidbuildEdgeList(intcnt,dcPt*pts,Edge*edges[]){Edge*edge;dcPtv1,v2;inti,yPrev=pts[cnt-2].
6、y;v1.x=pts[cnt-1].x;v1.y=pts[cnt-1].y;for(i=0;i7、edges);}yPrev=v1.y;v1=v2;}}voidbuildActiveList(intscan,Edge*active,Edge*edges[]){Edge*p,*q;p=edges[scan]->next;while(p){q=p->next;insertEdge(active,p);p=q;}}voidfillScan(intscan,Edge*active){Edge*p1,*p2;inti;p1=active->next;while(p1){p2=p1->next;for(i=p1->xInters
8、ect;ixIntersect;i++)setPixel((int)i,scan);p1=p2->next;}}voiddeleteAfter(Edge*q){Edge*p=q->next;q->next=p->next;free(p);}/*Deletecompletededges.Update'xInt