您好,欢迎访问三七文档
当前位置:首页 > 建筑/环境 > 工程监理 > bresenham画多边形
#includegl/glut.h#includeiostreamusingnamespacestd;voidsetPixel(intx,inty,floatr,floatg,floatb){glColor3f(r,g,b);glBegin(GL_POINTS);glVertex2i(x,y);glEnd();}voidmydisplay(intx0,inty0,intx1,inty1){glClearColor(0.0,1.0,1.0,0);glClear(GL_COLOR_BUFFER_BIT);intdatax,datay,p0;doublem;if(x1x0)//ÒÔÏÂΪbresenhamËã·¨»Ö±Ïß{inta,b;a=x1;b=y1;x1=x0;y1=y0;x0=a;y0=b;}datax=x1-x0;datay=y1-y0;m=(double)datay/(double)datax;setPixel(x0,y0,1.0,1.0,0);setPixel(x1,y1,1.0,1.0,0);if(m=0&&m=1){p0=2*datay-datax;inty=y0;intx=x0+1;for(x;xx1;x++){if(p00){p0=p0+2*datay-2*datax;y=y+1;setPixel(x,y,1.0,1.0,0);}else{p0=p0+2*datay;setPixel(x,y,1.0,1.0,0);}}}elseif(m=-1&&m0){p0=2*datay+datax;inty=y0;intx=x0+1;for(x;xx1;x++){if(p00){p0=p0+2*datay;setPixel(x,y,1.0,1.0,0);}else{p0=p0+2*datay+2*datax;y=y-1;setPixel(x,y,1.0,1.0,0);}}}elseif(m1){p0=2*datax-datay;intx=x0;inty=y0+1;for(y;yy1;y++){if(p00){p0=p0+2*datax-2*datay;x=x+1;setPixel(x,y,1.0,1.0,0);}else{p0=p0+2*datax;setPixel(x,y,1.0,1.0,0);}}}else{p0=-2*datax-datay;intx=x0;inty=y0-1;for(y;yy1;y--){if(p00){p0=p0-2*datax;setPixel(x,y,1.0,1.0,0);}else{p0=p0-2*datax-2*datay;x=x+1;setPixel(x,y,1.0,1.0,0);}}}glFlush();}}voidmain(){glutInitWindowSize(500,500);glutCreateWindow(¼òµ¥Ê¾Àý);gluOrtho2D(0.0,500.0,0.0,500.0);coutÇëÊäÈë±ßÊýСÓÚµÈÓÚ50µÄ¶à±ßÐαßÊýn;//¶¨Òå¶à±ßÐεıßÊýintn,k;cinn;inta[50],b[50];for(k=0;kn;k++){coutÊäÈ붥µã×ø±êx,y;intx,y;cinxy;a[k]=x;b[k]=y;}for(k=0;kn;k++){if(k==(n-1)){x0=a[k];y0=b[k];x1=a[0];y1=b[0];}else{x0=a[k];y0=b[k];x1=a[k+1];y1=b[k+1];}glutDisplayFunc(mydisplay(x0,y0,x1,y1));}glutMainLoop();}voidFillPloygon(){typedefstructEdge{intymax;floatx,deltax;//Edge*nextEdge}staticEdge[50];}
本文标题:bresenham画多边形
链接地址:https://www.777doc.com/doc-6052369 .html