How to write BresenHam’s Line Drawing Algorithm

To write a Source Code in c program to implement Bresenham’s line drawing algorithm.
Step by Step ALGORITHM
1. Include the graphics header file and obtain graphics mode and driver.
2. Get the co-ordinates of two end points of a line (x1,y1) & (x2,y2) and store left end point in (x1,y1).
3. Load (x1,y1) into frame buffer that means plot the first point.
4. Calculate constants dx,dy,2dy,2dy-2dx and obtain value for decision parameter p=2dy-dx.
5. At each x along the line perform the test if p<0, next point to plot is (x+1,y) and p+1=p+2dy otherwise (x+1,y+1) & p+1=p+2dy-2dx 6. Repeat steps dx times. 7. Display a line. |BresenHam’s Line Drawing Algorithm computer Graphics Source Code Programming in Computer Graphics #include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int x,y,x1,y1,x2,y2,gx,gy,dx,dy,m,i,p;
clrscr();
printf("Enter the starting point of line\n");
scanf("%d%d",&x1,&y1);
printf("Enter the end point of the line\n");
scanf("%d%d",&x2,&y2);
detectgraph(&gx,&gy);
initgraph(&gx,&gy,"d:\\tc\\bgi");
dx=x2-x1;
dy=y2-y1;
p=(2*dy)-dx;
for(i=1;i<dx;i++)
{
if(p<0)
{
x=x1++;
y=y1;
putpixel(x,y,30);
p=p+(2*dy);
}
else
{
x=x1++;
y=y1++;
putpixel(x,y,30);
p=p+(2*dy)-(2*dx);
}
}
getch();
}
Example Result OUTPUT | BRESENHAM’S LINE DRAWING ALGORITHM
Enter the starting point of line
0
0
Enter the end point of the line
200
200

Post a Comment

0 Comments