## Thursday, June 13, 2019

Bresenham line drawing algorithm

Serious drawback of DDA Algorithm is that It is very time consuming and it deals with the rounded of operation and floating point arithmetic.
The Algorithm developed by Brasenham’s is much more accurate and efficient as compare to DDA algorithm .
The algorithm uses only integer addition subtraction and multiplication by 2.
The basic principle of Bresenham’s algorithm. Is to find the optimum raster location. Raster location to represent as strait line to accomplish this algorithm always implement either x or b y slope 1 unit depending on the slop of the line interval. At sample position xk+1 the vertical point on y axis from the mathematical. Line are leveled as d-upper and d-lower from the above equation the y coordinate on the mathematical line at xk+1 as

y = m xk+1 + c

so, d-upper and d-lower can be calculated as

d-upper = yk+1 - y
d-lower = y - yk

so, with the use of following steps we can draw a line according to Bresenham’s algorithm.
1.       Input the two line and point and store the first end point in x0,y0 .
2.       Load x0,y0  into the buffer that is plot the first point.
3.       Calculate constant del x,del y and option the starting value for the decision parameter as

P=2 del y – del x

4.       At each xk  along the line starting at k=0 perform the following text if pk is less then 0 the next point to ploat is xk+1, yk and calculate the parameter as

pk+1 = pk + 2del y – 2del x

5.       Repeat the step- 4 until  the line will finished.

Program: -

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
Int gd, gm, x, y, end, p, x1, x2, y1, dx, ay;
detectgraph(&gd, &gn);
initgraph(&gd, &gm, “c://tc3//bgi);
printf(“enter the value of x1 & y1:”);
scanf(“%d %d”,&x1,&y1);
printf(“enter the value of x2 & y2:”);
scanf(“%d %d”,&x2,&y2);
dx=abs(x1-x2);
dy=abs(y1-y2);
p=2*dy-dx;
if(x1>x2)
{
x=x2;
y=y2;
end=x1;
}
else
{
x=x2;
y=y2;
end=x2;
}
while(x2=end)
{
if(p<0)
{
x++;
p=p+2*dy;
}
else
{
x++;
y++;
p=p+2*(dy-dx);
}
Putpixel(x,y,WHITE);
}
getch();
}