Friday, August 17, 2007

DDA Line Implementation

/* DDA Line Implementation - Subhranath Chunder */

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

void lineDDA(int,int,int,int);
void setPixel(int,int);

void main()
{
int x1,y1,x2,y2;
int driver=DETECT,mode;

clrscr();
printf("Enter the co-ordinates of the first point (a b): ");
scanf("%d %d",&x1,&y1);
printf("Enter the co-ordinates of the second point (a b): ");
scanf("%d %d",&x2,&y2);
printf("<Press any key to continue>");
getch();

initgraph(&driver,&mode,"F:\\TC\\BGI");
lineDDA(x1,y1,x2,y2);
getch();
closegraph();
}

void lineDDA(int x1,int y1,int x2,int y2)
{
float m,x,y;
x=x1;
y=y1;
setPixel(x,y);
if(x1==x2)
{
while(y!=y2)
{
if(y2-y1>0)
++y;
else
--y;
setPixel(x,y);
}
}
else
{
m=(float)(y2-y1)/(x2-x1);
if(m<=1 && m>=-1)
{
while(x!=x2)
{
if(x2-x1>0)
{
y=y+m;
++x;
}
else
{
y=y-m;
--x;
}
setPixel(x,y);
}
}
else
{
while(y!=y2)
{
if((y2-y1)>0)
{
x=x+(1/m);
++y;
}
else
{
x=x-(1/m);
--y;
}
setPixel(x,y);
}
}
}
}

void setPixel(int x,int y)
{
putpixel(x,y,2);
}




1 comment:

Zubilu Najmal said...

thank u for giving such a help