當前位置:編程學習大全網 - 編程軟體 - 怎麽使用DDA算法完成程序

怎麽使用DDA算法完成程序

#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+k; if k<0, y+1 and x+1/k.

*/

xIncrement = dx/(float)steps;

yIncrement = dy/(float)steps;

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

for (i=0; i<steps; i++) {

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;

}

  • 上一篇:阿裏和字節哪個更難進
  • 下一篇:音樂編輯插件WAVES作用到底大不大?
  • copyright 2024編程學習大全網