The DDA algorithm draws a straight line, its theoretical basis: if M represents a straight line slope, then there is the slope m = (y2-y1)/(X2-X1),
∴m =δy/δx, thus having any increment Δx for the given x along the line, calculates the increment of the corresponding y Δy = M Δx; the same: Δx =δy/m;
Assuming Slope |m| <= 1; That is, the length of x is greater than the length of Y. The Y values are calculated on a per-increment basis by X-unit spacing, with: yk+1 = yk + M;
Similarly, for |m| > 1; We have: xk+1 = YK + 1/m;
(Note: M is a slope, can be negative);
With the above mathematical basis, the process of drawing the line should be very clear. If there are two points (x1, y1) and (x2, y2); Calculate the Detax and Detay, the absolute value is large to determine the number of steps we want to draw steps. Increase the increment m in order to achieve the purpose of our drawing line,
The code is as follows:
1 //gltest.cpp: Defines the entry point of the console application. 2 //3#include"StdAfx.h"4#include"stdio.h"5#include <iostream>6 #pragmaComment (lib, "Glew32.lib")7#include <gl/glew.h>8#include <gl/glut.h>9 Ten using namespacestd; One A Static voidDda_draw_line (intX0,intY0,intX1,inty1); - voidRenderscene (void); - Static intRoundConst floatx); the Static voidDrawpixel (intXinty); - Static voidDraw_init (void); - - voidRenderscene (void) + { -Glclearcolor (1,1,1,1); +Glclear (Gl_color_buffer_bit |gl_depth_buffer_bit); ADda_draw_line (Ten, - the, $, Max); at //Drawpixel (+); - } - - Static intRoundConst floatx) - { - return(int) (x +0.5); in } - to Static voidDrawpixel (intXinty) + { - Glbegin (gl_points); the glvertex2i (x, y); * glend (); $ }Panax Notoginseng - Static voidDraw_init (void) the { + Glmatrixmode (gl_projection); AGluortho2d (- -, -, - -, -); theGlpointsize (2.0f); +GLCOLOR3F (1,0,0); - } $ $ Static voidDda_draw_line (intX0,intY0,intX1,inty1) - { - intDX = ABS (x1-x0); the intDY = ABS (y1-y0); - floatsteps;Wuyi floatx = x0, y =y0; the floatAD; - floatxincrement, yincrement; Wu -cout<<"DX:"<<DX <<", dy:"<<dy <<Endl; About $ if(DX >=dy) - { -Steps =DX; - } A Else + { theSteps =dy; - } $ thecout<<"Step:"<< Steps <<Endl; the thexincrement = DX/steps; theYincrement = dy/steps; - incout<<"XIn:"<< xincrement <<", YIn:"<< yincrement <<Endl; the the Drawpixel (Round (x), round (y)); About for(inti =0; i < steps; i++) the { the //cout<< "x:" <<x << ", y:" <<y<<endl; theX + =xincrement; +Y + =yincrement; - Drawpixel (Round (x), round (y)); the }Bayi glutswapbuffers (); the the } - - intMainintargcChar*argv[]) the { theGlutinitwindowsize ( -, -); theGlutinitwindowposition ( -, $); theGlutinitdisplaymode (Glut_rgb |glut_double); -Glutinit (&argc, argv); theGlutcreatewindow ("Hello OpenGL"); the the draw_init ();94 the theprintf"opengl version;%s\n", Glgetstring (gl_version)); theprintf"GLSL version:%s\n", Glgetstring (gl_shading_language_version));98 About Glutdisplayfunc (Renderscene); - Glutmainloop ();101 return 0;102 103}
The "DDA" algorithm for drawing line algorithm in graphic science