DDA算法原理:直线的一阶导是连续的,Δx和Δy是成比例的,有xi+1=xi+ε·Δx,yi+1=yi+ε·Δy. ε=1/max(|Δx|,|Δy|);
|k|<=1时,xi+1=xi+/-1;yi+1=yi+/-k;
|k|>=1时,max(|Δx|,|Δy|)=|Δy|,xi+1=xi+ε·Δx=xi+Δx/|Δy|=xi+/- 1/k;
yi+1=yi+/- 1;
对求出的xi+1和yi+1进行四舍五入。
round(xi+1) = (int)(xi+1+0.5),round(yi+1)=(int)(yi+1+0.5).
DDA算法程序:
1 void DDALine(int x1,int y1,int x2,int y2,int color){ 2 int dx,dy,epsl,k; 3 float x,y,xIncre,yIncre; 4 dx=x2-x1; dy=y2-y1; 5 x=x1; y=y1; 6 if(abs(dx)>abs(dy)) epsl = abs(dx); 7 else eps = abs(dy); 8 xIncre = (float)dx/(float)epsl; 9 yIncre = (float)dy/(float)epsl; 10 for(k=0;k<=epsl;k++){ 11 putpixel(int(x+0.5),(int)(y+0.5)); 12 x+=xIncre; 13 y+=yIncre; 14 } 15 }
来源:https://www.cnblogs.com/benbencoding798/p/4875317.html