怎么使用DDA算法完成程序

怎么使用DDA算法完成程序,第1张

#include <graphics.h>锋空

#include <math.h>

#define ROUND(a) ((int)(a+0.5))

#define OX 320

#define OY 240

void lineDDA (int xa, int ya, int xb, int yb, int color)

void setpixel (int x, int y, int color)

main(){

int gdrive=DETECT, gmode=0

initgraph(&gdrive, &gmode, "银档瞎d:\\tc")

setbkcolor(BLACK)

line (0, OY, 2*OX, OY)

line (OX, 0, OX, 2*OY)

lineDDA (100, 10, 0, 0, RED)

getch ()

closegraph()

return 0

}

/*

* DDA, digital differential analyzer, algoritm to calculating pixel position.

* Donald Hearn &M. Pauline Baker, Computer Graphics: C Version,

* Second Edition, 清华大学出版社, 2004, p88

*/

void lineDDA (int xa, int ya, int xb, int yb, int color) {

int dx = xb - xa

int dy = yb - ya

int steps, i

float xIncrement, yIncrement

float x=xa

float y=ya

if(abs(dx) >蠢顷 abs(dy))

steps = abs(dx)

else

steps = abs(dy)

/*

* y=kx+b, if k>0, x+1 and y+kif k<0, y+1 and x+1/k.

*/

xIncrement = dx/(float)steps

yIncrement = dy/(float)steps

putpixel (ROUND(x), ROUND(y), color)

for (i=0i<stepsi++) {

x += xIncrement

y += yIncrement

setpixel (ROUND(x), ROUND(y), color)

}

return

}

void setpixel (int x, int y, int color) {

/*printf ("(%d,%d)", x, y)

*/putpixel (OX+x, OY-y, color)

return

}

function DDA(x1,y1,x2,y2,color)

length =abs(x2-x1)

if abs(y2-y1)>length

length=abs(y2-y1)

end

dx=(x2-x1)/length

dy=(y2-y1)/length

x=x1+0.5*sign(dx)

y=y1+0.5*sign(dy)

hold on

for i=1:length

plot(round(x),round(y),'Color',color)

x=x+dx

y=y+dy

end

hold off

end


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/8200859.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-14
下一篇 2023-04-14

发表评论

登录后才能评论

评论列表(0条)

保存