Cómo utilizar el algoritmo DDA para completar el programa
#include
#include
#define ROUND(a) ((int)(a+0.5) )
#define OX 320
#define OY 240
línea vacíaDDA (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(NEGRO);
line (0, OY, 2*OX, OY);
línea (OX, 0, OX, 2*OY);
líneaDDA (100, 10, 0, 0, RED);
getch () ;
closegraph();
devuelve 0;
}
/*
* DDA, digital analizador diferencial, algoritmo para calcular la posición de los píxeles.
* Donald Hearn & M. Pauline Baker, Computer Graphics: C Version,
* Segunda edición, Tsinghua University Press, 2004, p88
*/
línea vacíaDDA (int xa, int ya, int xb, int yb, int color) {
int dx = xb - xa;
int dx = xb - xa;
p>int dy = yb - ya;
int pasos, i;
float xIncrement, yIncrement;
float x=xa;< / p>
float y=ya;
if(abs(dx) > abs(dy))
pasos = abs(dx);
else
pasos = abs(dy);
/*
* y=kx+b, si k>0, x+1 e y+ k; si k<0, y+1 y x+1/k.
*/
xIncremento = dx/pasos (flotantes);
yIncremento = dy /(flotante)pasos;
putpixel (ROUND(x), ROUND(y), color);
for (i=0; i x += xIncremento; y += yIncremento; setp ixel (REDONDO(x), REDONDO(y), color); } return; } void setpixel ( int x, int y, int color) { /* printf ("(%d,%d)", x, y); */ putpixel (OX+x , OY-y, color); return; }