怎么使用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

}

这个函数需要自己进行定义,意思是画出一个坐标为(x,y)的点,定义如下:

void setPixel(int x, int y) //定义一个函数,参数为x和y,返回值为空

{

glBegin(GL_POINTS)

glVertex2i(x,y)

glEnd()

}//整体这个函数的意思是:画一个坐标为(x,y)的点


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

原文地址: http://outofmemory.cn/yw/12143555.html

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

发表评论

登录后才能评论

评论列表(0条)

保存