Saturday, September 10, 2011

C Graphics : DDA Line Draw

#include <graphics.h>
#include <math.h>
#include <stdio.h>
#include <conio.h>

void dda_line(int x1,int y1,int x2,int y2);

int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;

/* initialize graphics mode */
initgraph(&gdriver, &gmode, "c:\\tc");

/* read result of initialization */
errorcode = graphresult();

if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* return with error code */
}

getch();
return(0);
}

void dda_line(int x1,int y1,int x2,int y2)
{
int dx,dy,step;
float x,y,ix,iy;
dx=x2-x1;
dy=y2-y1;

if(abs(dx)>abs(dy))
step=abs(dx);
else
step=abs(dy);

ix=dx/(float)step;
iy=dy/(float)step;

x=x1;
y=y1;
putpixel(x,y,1);
while(step-->0)
{
x=x+ix;
y=y+iy;

putpixel(x,y,1);
}

}

No comments:

Post a Comment