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

A diagram illustrating Bresenham's line algorithm.

Image via Wikipedia

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 (x0y0) and (x1y1), 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 (x0x1 and y0y1), and its horizontal projection x1 − x0 is longer than the vertical projection y1 − y0 (the line has a slope whose absolute value is less than 1 and greater than 0.) In this octant, for each column x between x0 and x1, there is exactly one row y (computed by the algorithm) containing a pixel of the line, while each row between y0 and y1 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:

\frac{y - y_0}{y_1-y_0} = \frac{x-x_0}{x_1-x_0}.

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

y = \frac{y_1-y_0}{x_1-x_0} (x-x_0) + y_0.

The slope (y1 − y0) / (x1 − x0) 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 y0and 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

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s