Bresenham line drawing algorithm
My code for Bresenham line drawing algorithm which will help you to learn how the algorithm actually works.
Source code:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void bresenham(int x1,int y1,int x2,int y2);
void main()
{int gd=DETECT,gm;
int x1,x2,y1,y2;
initgraph( &gd, &gm,"C:\\TC\\BGI");
cleardevice();
cout<<"Bresenham Line generation Algorithm";
cout<<"\n enter the starting co-ordinates for
drawing line";
cin>>x1>>y1;
cout<<"\n enter the ending co-ordinates";
cin>>x2>>y2;
bresenham(x1,y1,x2,y2);
cout<<"\n Thank you";
getch();
closegraph();
}
void bresenham(int x1,int y1,int x2,int y2)
{int x,y,dx,dy,e,i;
if(x1>x2)
{bresenham(x2,y2,x1,y1);
return;}
dx=x2-x1;
dy=y2-y1;
e=2*dy-dx;
x=x1;
y=y1;
putpixel(x,y,WHITE);
cout<<"("<<x<<","<<y<<")";
getch();
for(i=1;i<=dx;i++)
{while(e>=0)
{y=y+1;
e=e-2*dx;}
x=x+1;
e=e+2*dy;
putpixel(x,y,WHITE);
cout<<"("<<x<<","<<y<<")";
getch();
}
}
OUTPUT:
No comments:
Post a Comment