Development Environment:
Vc++6.0,opengl
Experimental content:
Use the DDA algorithm to draw a straight line.
Experimental Results:
Code:
1#include <gl/glut.h>2#include <math.h>3 4 #defineWIDTH 500//window Width5 #defineHEIGHT 500//Window Height6 7 #defineDRAWLINE1 Ddaline (100,200,200,100);//Draw a line8 #defineDRAWLINE2 Ddaline (200,100,450,400);//Draw a line9 Ten #pragmaComment (linker, "/subsystem:\" windows\ "/entry:\" Maincrtstartup\ "")//Canceling the console One A voidInit ()//Initialize - { -Glclearcolor (1.0f,1.0f,1.0f,1.0f);//Set background color, completely opaque theGLCOLOR3F (1.0f,0.0f,0.0f);//Set Brush Color - -Glmatrixmode (gl_projection);//Set Projection -Gluortho2d (0.0, WIDTH,0.0, HEIGHT);//Setting the projection area + } - + voidDdaline (intX0,intY0,intX1,intY1)//DDA algorithm draw line A { at intdx,dy,eps1,i; - floatX,y,xincre,yincre; -DX = x1-x0; -DY = y1-y0; -x = X0,y =y0; - if(ABS (DX) >abs (DY)) inEPS1 =ABS (DX); - Else toEPS1 =abs (DY); +Xincre = (float) DX/float(EPS1); -Yincre = (float) dy/float(EPS1); the for(i=0; i<=eps1;i++){ * Glbegin (gl_points); $Glvertex2i (int(x+0.5),int(y+0.5));Panax Notoginseng glend (); -x+=Xincre; they+=Yincre; + } A } the + voidDisplay ()//Display Functions - { $Glclear (Gl_color_buffer_bit);//Empty the color stack $ - DRAWLINE1 - DRAWLINE2 the -Glflush ();//emptying buffer DirectivesWuyi } the - intMainintargcChar**argv) Wu { -Glutinit (&argc,argv); AboutGlutinitdisplaymode (Glut_single | GLUT_RGB);//Initialize display mode $Glutinitwindowsize (Width,height);//Set Window Size -Glutinitwindowposition ( $, -);//Set Window Position -Glutcreatewindow ("Draw a line");//Create Window - AGlutdisplayfunc (Display);//Registering display functions +Init ();//Initialize theGlutmainloop ();//Enter program Loop - return 0; $}
freecode:www.cnblogs.com/yym2013
"Graphic Science" experiment Three: DDA algorithm draw straight line