Wednesday, 16 April 2014

C++ program to draw a line using putpixel

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

main()
{
int gd=DETECT,gm,x2,y2;
float x1,y1,m,b;
initgraph(&gd,&gm,"C:\\turboc3\\bgi");
cout<<"enter starting points";
cin>>x1>>y1;
cout<<"enter ending points";
cin>>x2>>y2;
 m=(y2-y1)/(x2-x1);
b=y1-(m*x1);
if(m<1)
{
while(x1<=x2&&y1<=y2)
{
putpixel(x1,y1,WHITE);
x1=x1+1;
y1=(m*x1)+b;
}
}
if(m>1)
{
while(x1<=x2&&y1<=y2)
{
putpixel(x1,y1,WHITE);
y1=y1+1;
x1=(y1-b)/m;
}
}
getch();
closegraph();
}

No comments:

Post a Comment