Pages

Mar 2, 2013

wap in c++ to draw a line using DDA Algorithm


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

void main()
{
int gdriver=DETECT, gmode;
initgraph(&gdriver,&gmode, "");
int x1,x2,y1,y2,dx,dy,m;

cout<<"Enter Values for x1,x2,y1,y2: ";
cin>>x1>>x2>>y1>>y2;

while(x1!=x2 && y1!=y2)
{
if(x1<x2)
{
dx = x2-x1;  dy = y2-y1; m=dy/dx;
putpixel(x1,y1,RED);

if(m<=1)
{
x1++;
y1 = y1+m;
}
if(m>1)
{
y1++;
x1 = x1 + (1/m);
}
putpixel(x1,y1,RED);
}
if(x2<x1)
{
dx = x2-x1;  dy = y2-y1; m=dy/dx;
putpixel(x1,y1,RED);

if(m<=1)
{
x1--;
y1 = y1-m;
}
if(m>1)
{
y1--;
x1 = x1 - (1/m);
}
putpixel(x1,y1,RED);
}

}

getch();
}

No comments:

Post a Comment