Tuesday, 16 July 2013

Draw a line using Bresenham's algorithm in Computer Graphic








 Draw a line using Bresenham's algorithm

#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void linebres(int x1,int y1,int x2,int y2)
{
int dx=abs(x1-x2),dy=abs(y1-y2);
int twody=2*dy,twodydx=2*(dy-dx);
int p=2*dy-dx;
int x,y,xend;
if(x1>x2)
{
x=x2;
y=y2;
xend=x1;
}
else
{
x=x1;
y=y1;
xend=x2;
}
putpixel(x,y,12);
while(x<xend)
{
x++;
if(p<0)
p+=twody;
else
{
p+=twodydx;
y++;
}
putpixel(x,y,12);
}
}
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"d:\\turboc3");
int x1,x2,y1,y2;
cout<<"Enter Initial co-ordinates:(x1,y1)";
cout<<"\nx-axis:";
cin>>x1;
cout<<"y-axis:";
cin>>y1;
cout<<"Enter End co-ordinates:(x2,y2)";
cout<<"\nx-axis:";
cin>>x2;
cout<<"y-axis:";
cin>>y2;
linebres(x1,y1,x2,y2);
getch();
}



Output:
Enter Initial co-ordinates:(x1,y1)
x-axis:  100
y-axis:     150
Enter End co-ordinates:(x2,y2)
x-axis:  500
y-axis:     150
           



 









No comments: