資源描述:
《DDA算法 中點(diǎn)畫(huà)線(xiàn)算法 Bresenham算法》由會(huì)員上傳分享,免費(fèi)在線(xiàn)閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫(kù)。
1、實(shí)驗(yàn)1直接繪制實(shí)驗(yàn)(提示:#表示Project的編號(hào),##表示Project題目)學(xué)號(hào)姓名上交時(shí)間題號(hào)程序邏輯(40)算法新穎性(20)代碼規(guī)范(20)實(shí)驗(yàn)報(bào)告(20)總分得分1.問(wèn)題描述如何利用OpenGL實(shí)現(xiàn)直線(xiàn)光柵化的DDA算法、中點(diǎn)畫(huà)線(xiàn)算法和Bresenham算法2.算法描述DDA算法:據(jù)直線(xiàn)公式y(tǒng)=kx+b來(lái)推導(dǎo)出來(lái)的,其關(guān)鍵之處在于如何設(shè)定單位步進(jìn),即一個(gè)方向的步進(jìn)為單位步進(jìn),另一個(gè)方向的步進(jìn)必然是小于1。中點(diǎn)劃線(xiàn)法:?在畫(huà)直線(xiàn)段的過(guò)程中,當(dāng)前像素點(diǎn)為(xp?,yp?),下一個(gè)像素點(diǎn)有兩種可選擇點(diǎn)P1(xp?+1,yp?)或P2(xp?+1,
2、yp?+1)。若M=(xp?+1,yp?+0.5)為P1與P2之中點(diǎn),Q為P理想直線(xiàn)與x=xp?+1垂線(xiàn)的交點(diǎn)。當(dāng)M在Q的下方,則P2應(yīng)為下一個(gè)像素點(diǎn);M在Q的上方,應(yīng)取P1為下一個(gè)像素點(diǎn)。Bresenham算法:過(guò)各行、各列像素中心構(gòu)造一組虛擬網(wǎng)格線(xiàn),按直線(xiàn)從起點(diǎn)到終點(diǎn)的順序計(jì)算直線(xiàn)各垂直網(wǎng)格線(xiàn)的交點(diǎn),然后確定該列像素中與此交點(diǎn)最近的像素。實(shí)驗(yàn)結(jié)果成功運(yùn)行三個(gè)算法,并且能轉(zhuǎn)換出通用Bresenham算法。3.分析與評(píng)論(分析每個(gè)算法的運(yùn)行時(shí)間,對(duì)你的本實(shí)驗(yàn)的工作進(jìn)行評(píng)論,同時(shí)也可以對(duì)老師提出建議。)附錄:SourceCode(inC)4#include
3、//需要正確安裝GLUT,安裝方法如預(yù)備知識(shí)中所述voidmyDisplay(void){glClearColor(0.0,0.0,0.0,0.0);glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0f,1.0f,1.0f);glRectf(-0.5f,-0.5f,0.5f,0.5f);glBegin(GL_TRIANGLES);glColor3f(1.0f,0.0f,0.0f);glVertex2f(0.0f,1.0f);glColor3f(0.0f,1.0f,0.0f);glVertex2f(0
4、.8f,-0.5f);glColor3f(0.0f,0.0f,1.0f);glVertex2f(-0.8f,-0.5f);glEnd();glColor3f(1,0,0);glBegin(GL_LINE_LOOP);glVertex2f(0.0f,0.5f);glVertex2f(0.4f,-0.25f);glVertex2f(-0.4f,-0.25f);glEnd();glPointSize(3);glBegin(GL_POINTS);glColor3f(1.0f,0.0f,0.0f);glVertex2f(-0.4f,-0.4f);glColor3f
5、(0.0f,1.0f,0.0f);glVertex2f(0.0f,0.0f);glColor3f(0.0f,0.0f,1.0f);glVertex2f(0.4f,0.4f);glEnd();glFlush();}intmain(intargc,char*argv[]){glutInit(&argc,argv);glutInitDisplayMode(GLUT_RGB
6、GLUT_SINGLE);4glutInitWindowPosition(100,100);glutInitWindowSize(400,400);glutCreateWindow("Hel
7、loWorld!");glutDisplayFunc(&myDisplay);glutMainLoop();return0;}通用算法:intSign(intn){if(n>0)return1;if(n==0)return0;if(n<0)return-1;}voidBresenham(intx0,inty0,intx1,inty1,void(*setPixel)(intx,inty)){intx,y,dx,dy,s1,s2,temp=0,interchange;x=x0;y=y0;dx=abs(x1-x0);dy=abs(y1-y0);s1=Sign(
8、x1-x0);s2=Sign(y1-y0);if(dy>dx){temp=dx;dx=dy;dy=temp;interchange=1;}elseinterchange=0;inte=2*dy-dx;for(inti=1;i<=dx;++i){setPixel(x,y);while(e>0){if(interchange==1)x=x+s1;elsey=y+s2;e=e-2*dx;}if(interchange==1)y=y+s2;elsex=x+s1;e=e+2*dy;4}}(以上是實(shí)驗(yàn)報(bào)告的最小要求,以后可以會(huì)根據(jù)各個(gè)Project的不同情況增加內(nèi)容
9、。上交時(shí)排版要美觀。)4