program To Draw A Line Using Mid Point Algorithm
program To Draw A Line Using Mid Point Algorithm
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void main()
{
int gd = DETECT, gm;
int x0, x1, y0, y1,dx,dy;
float p;
initgraph(&gd,&gm,"c:/tc/bgi");
cout<<"Enter the end points of the line(x1,y1 and x2,y2): "<<endl;
cout<<"Enter starting point"<<endl;
cout<<"x1: ";
cin>>x0;
cout<<"y1: ";
cin>>y0;
cout<<"Enter ending point "<<endl;
cout<<"x2: ";
cin>>x1;
cout<<"y2: ";
cin>>y1;
clrscr();
dx=x1-x0;
dy=y1-y0;
p=2*dy-dx;
int y=y0;
for(int i=x0;i<=x1;i++)
{
if(p<0)
{ putpixel(i,y,10);
p=p+2*dy;
}
else
{ y++;
putpixel(i,y,10);
p=p+2*dy-2*dx;
}
}
getch();
closegraph();
}
OUTPUT:
Enter the end points of the line(x1,y1 and x2,y2):
Enter starting point
x1 : 20
y1 : 20
Enter ending point
x2 : 400
y2 : 400