Bresenham's line algorithm is a line drawing algorithm that determines the points of an n-dimensional raster that should be selected in order to form a close approximation to a straight line between two points. It is commonly used to draw line primitives in a bitmap image (e.g. on a computer screen), as it uses only integer addition, subtraction, and bit shifting, all of which are very cheap operations in historically common computer architectures. It is an incremental error algorithm, and one of the earliest algorithms developed in the field of computer graphics. An extension to the original algorithm called the midpoint circle algorithm may be used for drawing circles. While algorithms such as Wu's algorithm are also frequently used in modern computer graphics because they can support antialiasing, Bresenham's line algorithm is still important because of its speed and simplicity. 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 software graphics libraries. Because the algorithm is very simple, it is often implemented in either the firmware or the graphics hardware of modern graphics cards. The label "Bresenham" is used today for a family of algorithms extending or modifying Bresenham's original algorithm.
History Bresenham's line algorithm is named after Jack Elton Bresenham who developed it in 1962 at IBM. In 2001, Bresenham wrote:
I was working in the computation lab at IBM's San Jose development lab. A Calcomp plotter had been attached to an IBM 1401 via the 1407 typewriter console. [The algorithm] was in production use by summer 1962, possibly a month or so earlier. Programs in those days were freely exchanged among corporations so Calcomp (Jim Newland and Calvin Hefte) had copies. When I returned to Stanford in Fall 1962, I put a copy in the Stanford comp center library. A description of the line drawing routine was accepted for presentation at the 1963 ACM national convention in Denver, Colorado. It was a year in which no proceedings were published, only the agenda of speakers and topics in an issue of Communications of the ACM. A person from the IBM Systems Journal asked me after I made my presentation if they could publish the paper. I happily agreed, and they printed it in 1965.
Method
The following conventions will be applied:
the top-left is (0,0) such that pixel coordinates increase to the right and downwards (e.g. that the pixel at (7,4) is directly above the pixel at (7,5)), and the pixel centers have integer coordinates. The endpoints of the line are the pixels at (x0,y0) and (x1,y1), 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 (x0≤x1 and y0≤y1), and its horizontal projection x1−x0 is longer than the vertical projection y1−y0 (the line has a positive slope less than 1). 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:
y−y0y1−y0=x−x0x1−x0. Since we know the column, x, the pixel's row, y, is given by rounding this quantity to the nearest integer:
y=y1−y0x1−x0(x−x0)+y0. The slope (y1−y0)/(x1−x0) only depends on the endpoint coordinates and can be precomputed, and the ideal y for successive integer values of x can be computed starting from y0 and repeatedly adding the slope. In practice, the algorithm does not keep track of the y coordinate, which increases by m = ∆y/∆x each time the x increases by one; it keeps an error bound at each stage, which represents the negative of the distance from (a) the point where the line exits the pixel to (b) the top edge of the pixel. This value is first set to y0−0.5 (due to using the pixel's center coordinates), and is incremented by m each time the x coordinate is incremented by one. If the error becomes greater than 0.5, we know that the line has moved upwards one pixel, and that we must increment our y coordinate and readjust the error to represent the distance from the top of the new pixel – which is done by subtracting one from error.
Derivation To derive Bresenham's algorithm, two steps must be taken: first, the equation of a line is transformed from the typical slope-intercept form into an implicit equation with integer coefficients; second, this new equation is used to draw the line based on the idea of error accumulation.
Line equation
The slope-intercept form of a line is written as
y=f(x)=mx+b where m is the slope and b is the y-intercept. Because this is a function of only x, it can't represent a vertical line. Therefore, it would be useful to make this equation written as a function of both x and y to be able to draw lines at any angle. The slope of a line can be expressed as "rise over run", or Δy/Δx. Then, using algebraic manipulation,
y=mx+by=ΔyΔxx+b(Δx)y=(Δy)x+(Δx)b0=(Δy)x−(Δx)y+(Δx)b Letting this last equation be a function of x and y, it can be written as
f(x,y):=Ax+By+C=0 where the constants are
A=Δy=y1−y0 B=−Δx=−(x1−x0) C=(Δx)b=(x1−x0)b The line is then defined for some constants A, B, and C anywhere f(x,y)=0. That is, for any (x,y) not on the line, f(x,y)≠0. This form involves only integers if x and y are integers, since the constants A, B, and C are defined as integers. As an example, the line y=12x+1 could be written as f(x,y)=x−2y+2. The point (2,2) is on the line
f(2,2)=x−2y+2=(2)−2(2)+2=2−4+2=0 and the point (2,3) is not on the line
f(2,3)=(2)−2(3)+2=2−6+2=−2 and neither is the point (2,1)
f(2,1)=(2)−2(1)+2=2−2+2=2 Notice that the points (2,1) and (2,3) are on opposite sides of the line and f(x,y) evaluates to positive or negative. A line splits a plane into halves. The half-plane that has a negative f(x,y) can be called the negative half-plane, and the other half can be called the positive half-plane. This observation is very important for the remainder of the derivation.
Algorithm The starting point is on the line
… excerpt ends here. Continue reading the full article.




