The **Bresenham line algorithm** is an algorithm which determines which points in an n-dimensional raster should be plotted in order to form a close approximation to a straight line between two given points. It is commonly used to draw lines on a computer screen, as it uses only integer addition, subtraction and bit shifting, all of which are very cheap operations in standard computer architectures. It is one of the earliest algorithms developed in the field of computer graphics. A minor extension to the original algorithm also deals with drawing circles.

While algorithms such as Wu’s algorithm are also frequently used in modern computer graphics because they can support antialiasing, the speed and simplicity of Bresenham’s line algorithm mean that it is still important. The algorithm is used in hardware such as plotters and in the graphics chips of modern graphics cards. It can also be found in many softwaregraphics libraries. Because the algorithm is very simple, it is often implemented in either the firmware or the hardware of modern graphics cards.

## The algorithm

Illustration of the result of Bresenham’s line algorithm. (0,0) is at the top left corner and (12, 6) is at the bottom right corner.

The common conventions will be used:

- the top-left is (0,0) such that pixel coordinates increase in the right and down directions (e.g. that the pixel at (1,1) is directly above the pixel at (1,2)), and
- that the pixel centers have integer coordinates.

The endpoints of the line are the pixels at (*x*_{0}, *y*_{0}) and (*x*_{1}, *y*_{1}), where the first coordinate of the pair is the column and the second is the row.

The algorithm will be initially presented only for the octant in which the segment goes down and to the right (*x*_{0}≤*x*_{1} and *y*_{0}≤*y*_{1}), and its horizontal projection *x*_{1} − *x*_{0} is longer than the vertical projection *y*_{1} − *y*_{0} (the line has a slope whose absolute value is less than 1 and greater than 0.) In this octant, for each column *x* between *x*_{0} and *x*_{1}, there is exactly one row *y* (computed by the algorithm) containing a pixel of the line, while each row between *y*_{0} and *y*_{1} may contain multiple rasterized pixels.

Bresenham’s algorithm chooses the integer *y* corresponding to the pixel center that is closest to the ideal (fractional) *y* for the same *x*; on successive columns y can remain the same or increase by 1. The general equation of the line through the endpoints is given by:

Since we know the column, *x*, the pixel’s row, *y*, is given by rounding this quantity to the nearest integer:

The slope (*y*_{1} − *y*_{0}) / (*x*_{1} − *x*_{0}) depends on the endpoint coordinates only and can be precomputed, and the ideal *y* for successive integer values of *x* can be computed starting from *y*_{0}and repeatedly adding the slope.

## Implementation of Algorithm

#include<iostream.h>

#include<conio.h>

#include<graphics.h>

void main()

{

clrscr();

int gdriver = DETECT,gmode;

initgraph(&gdriver,&gmode,”c:/tc/bgi”);

int xa,xb,ya,yb,dx,dy,tdy,tdydx,p,x,y,xend;

cout<<“\n enter the starting points of line “;

cin>>xa>>ya;

cout<<“\n enter the ending points of line “;

cin>>xb>>yb;

dx=xa-xb;

dy=ya-yb;

p=2*(dy-dx);

tdy=2*dy;

tdydx=2*(dy-dx);

if(xa>xb)

{

x=xb;

y=yb;

xend=xa;

}

else

{

x=xa;

y=ya;

xend=xb;

}

putpixel(x,y,15);

while(x<xend)

{

x++;

if(p<0)

p+=tdy;

else

{

y++;

p+=tdydx;

}

putpixel(x,y,15);

}

getch();

}

Reference: wikipedia.org