HOME

TheInfoList



OR:

Bresenham's line algorithm is a
line drawing algorithm In computer graphics, a line drawing algorithm is an algorithm for approximating a line segment on discrete graphical media, such as pixel-based displays and printers. On such media, line drawing requires an approximation (in nontrivial cases). ...
that determines the points of an ''n''-dimensional
raster Raster may refer to: * Raster graphics, graphical techniques using arrays of pixel values * Raster graphics editor, a computer program * Raster scan, the pattern of image readout, transmission, storage, and reconstruction in television and compu ...
that should be selected in order to form a close approximation to a
straight line between two points In geometry, a line segment is a part of a line (mathematics), straight line that is bounded by two distinct end Point (geometry), points, and contains every point on the line that is between its endpoints. The length of a line segment is give ...
. It is commonly used to draw line primitives in a
bitmap image In computing, a bitmap is a mapping from some domain (for example, a range of integers) to bits. It is also called a bit array or bitmap index. As a noun, the term "bitmap" is very often used to refer to a particular bitmapping application: th ...
(e.g. on a
computer screen A computer monitor is an output device that displays information in pictorial or textual form. A discrete monitor comprises a visual display, support electronics, power supply, housing, electrical connectors, and external user controls. The di ...
), as it uses only integer addition, subtraction and
bit shifting In computer programming, a bitwise operation operates on a bit string, a bit array or a Binary numeral system, binary numeral (considered as a bit string) at the level of its individual bits. It is a fast and simple action, basic to the higher-l ...
, all of which are very cheap operations in commonly used computer
instruction sets In computer science, an instruction set architecture (ISA), also called computer architecture, is an abstract model of a computer. A device that executes instructions described by that ISA, such as a central processing unit (CPU), is called an ' ...
such as
x86_64 x86-64 (also known as x64, x86_64, AMD64, and Intel 64) is a 64-bit version of the x86 instruction set, first released in 1999. It introduced two new modes of operation, 64-bit mode and compatibility mode, along with a new 4-level paging mod ...
. It is an
incremental error algorithm This is a glossary of terms relating to computer graphics. For more general computer hardware terms, see glossary of computer hardware terms. 0–9 A B ...
, and one of the earliest algorithms developed in the field of
computer graphics Computer graphics deals with generating images with the aid of computers. Today, computer graphics is a core technology in digital photography, film, video games, cell phone and computer displays, and many specialized applications. A great de ...
. An
extension Extension, extend or extended may refer to: Mathematics Logic or set theory * Axiom of extensionality * Extensible cardinal * Extension (model theory) * Extension (predicate logic), the set of tuples of values that satisfy the predicate * E ...
to the original algorithm may be used for drawing
circles A circle is a shape consisting of all points in a plane that are at a given distance from a given point, the centre. Equivalently, it is the curve traced out by a point that moves in a plane so that its distance from a given point is const ...
. While algorithms such as Wu's algorithm are also frequently used in modern computer graphics because they can support
antialiasing Anti-aliasing may refer to any of a number of techniques to combat the problems of aliasing in a sampled signal such as a digital image or digital audio recording. Specific topics in anti-aliasing include: * Anti-aliasing filter, a filter used be ...
, Bresenham's line algorithm is still important because of its speed and simplicity. The algorithm is used in hardware such as
plotter A plotter is a machine that produces vector graphics drawings. Plotters draw lines on paper using a pen, or in some applications, use a knife to cut a material like vinyl or leather. In the latter case, they are sometimes known as a cutting pl ...
s and in the
graphics chips A video display controller or VDC (also called a display engine or display interface) is an integrated circuit which is the main component in a video-signal generator, a device responsible for the production of a TV video signal in a computin ...
of modern
graphics card A graphics card (also called a video card, display card, graphics adapter, VGA card/VGA, video adapter, display adapter, or mistakenly GPU) is an expansion card which generates a feed of output images to a display device, such as a computer moni ...
s. It can also be found in many
software Software is a set of computer programs and associated documentation and data. This is in contrast to hardware, from which the system is built and which actually performs the work. At the lowest programming level, executable code consists ...
graphics libraries A graphics library is a program library designed to aid in rendering computer graphics to a monitor. This typically involves providing optimized versions of functions that handle common rendering tasks. This can be done purely in software and runn ...
. Because the algorithm is very simple, it is often implemented in either the
firmware In computing, firmware is a specific class of computer software that provides the low-level control for a device's specific hardware. Firmware, such as the BIOS of a personal computer, may contain basic functions of a device, and may provide h ...
or the
graphics hardware Graphics hardware is computer hardware that generates computer graphics and allows them to be shown on a display, usually using a graphics card (video card) in combination with a device driver to create the images on the screen. Types Grap ...
of modern
graphics card A graphics card (also called a video card, display card, graphics adapter, VGA card/VGA, video adapter, display adapter, or mistakenly GPU) is an expansion card which generates a feed of output images to a display device, such as a computer moni ...
s. 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 Jack Elton Bresenham (born 11 October 1937, Clovis, New Mexico, US) is a former professor of computer science. Biography Bresenham retired from 27 years of service at IBM as a Senior Technical Staff Member in 1987. He taught for 16 years at Wi ...
who developed it in 1962 at IBM. In 2001 Bresenham wrote:Paul E. Black. ''Dictionary of Algorithms and Data Structures,''
NIST The National Institute of Standards and Technology (NIST) is an agency of the United States Department of Commerce whose mission is to promote American innovation and industrial competitiveness. NIST's activities are organized into physical sci ...
. https://xlinux.nist.gov/dads/HTML/bresenham.html
I was working in the computation lab at IBM's San Jose development lab. A
Calcomp plotter Calcomp plotters (sometimes referred to as CalComp plotters) were the best known products of the California Computer Products company ( Calcomp or CalComp). Overview The Calcomp 565 drum plotter, introduced in 1959, was one of the first compute ...
had been attached to an
IBM 1401 The IBM 1401 is a variable-wordlength decimal computer that was announced by IBM on October 5, 1959. The first member of the highly successful IBM 1400 series, it was aimed at replacing unit record equipment for processing data stored on pu ...
via the 1407 typewriter console. he algorithmwas 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.
Bresenham's algorithm has been extended to produce circles, ellipses, cubic and quadratic bezier curves, as well as native anti-aliased versions of those.Zingl, Alois "A Rasterizing Algorithm for Drawing Curves" (2012) http://members.chello.at/~easyfilter/Bresenham.pdf


Method

The following 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 (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 (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 \leq x_1 and y_0 \leq y_1), and its horizontal projection x_1-x_0 is longer than the vertical projection y_1-y_0 (the line has a positive
slope In mathematics, the slope or gradient of a line is a number that describes both the ''direction'' and the ''steepness'' of the line. Slope is often denoted by the letter ''m''; there is no clear answer to the question why the letter ''m'' is use ...
less than 1). 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 In digital imaging, a pixel (abbreviated px), pel, or picture element is the smallest addressable element in a raster image, or the smallest point in an all points addressable display device. In most digital display devices, pixels are the smal ...
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 = \frac. Since we know the column, ''x'', the pixel's row, ''y'', is given by rounding this quantity to the nearest integer: :y = \frac (x-x_0) + y_0. 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. 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 y_0-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. The first step is transforming the equation of a line from the typical slope-intercept form into something different; and then using this new equation to draw a line based on the idea of accumulation of error.


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 angle (or slope) of a line can be stated as "rise over run," or \Delta y/\Delta x. Then, using algebraic manipulation, : \begin y & = mx + b \\ y & = \frac x + b \\ (\Delta x) y & = (\Delta y) x + (\Delta x) b \\ 0 & = (\Delta y) x - (\Delta x) y + (\Delta x) b \end 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 = \Delta y = y_1 - y_0 * B = - \Delta x = - (x_1 - x_0) * C = (\Delta x) b = x_1 y_0 - x_0 y_1 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) \ne 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=\fracx + 1 then this 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 and 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 in the remainder of the derivation.


Algorithm

Clearly, the starting point is on the line :f(x_0, y_0) = 0 only because the line is defined to start and end on integer coordinates (though it is entirely reasonable to want to draw a line with non-integer end points). Keeping in mind that the slope is at most 1, the problem now presents itself as to whether the next point should be at (x_0 + 1, y_0) or (x_0 + 1, y_0 + 1). Perhaps intuitively, the point should be chosen based upon which is closer to the line at x_0 + 1. If it is closer to the former then include the former point on the line, if the latter then the latter. To answer this, evaluate the line function at the midpoint between these two points: :f(x_0 + 1, y_0 + \tfrac 1 2) If the value of this is positive then the ideal line is below the midpoint and closer to the candidate point (x_0+1,y_0+1); in effect the y coordinate has advanced. Otherwise, the ideal line passes through or above the midpoint, and the y coordinate has not advanced; in this case choose the point (x_0+1,y_0). The value of the line function at this midpoint is the sole determinant of which point should be chosen. The adjacent image shows the blue point (2,2) chosen to be on the line with two candidate points in green (3,2) and (3,3). The black point (3, 2.5) is the midpoint between the two candidate points.


Algorithm for integer arithmetic

Alternatively, the difference between points can be used instead of evaluating f(x,y) at midpoints. This alternative method allows for integer-only arithmetic, which is generally faster than using floating-point arithmetic. To derive the alternative method, define the difference to be as follows: : D = f(x_0+1,y_0+\tfrac 1 2) - f(x_0,y_0) For the first decision, this formulation is equivalent to the midpoint method since f(x_0,y_0)=0 at the starting point. Simplifying this expression yields: :\begin D & = & \left A(x_0+1) + B \left(y_0+\frac\right) + C \right& - & \left A x_0 + B y_0 + C \right\\ & = & \left Ax_0 + B y_0+ C + A + \frac B\right & - & \left A x_0 + B y_0 + C \right\\ & = & A + \frac B = \Delta y - \frac \Delta x \end Just as with the midpoint method, if D is positive, then choose (x_0+1,y_0+1), otherwise choose (x_0+1,y_0). If (x_0+1,y_0) is chosen, the change in D will be: :\begin \Delta D &=& f(x_0+2,y_0+\tfrac 1 2) - f(x_0+1,y_0+\tfrac 1 2) &=& A &=& \Delta y \\ \end If (x_0+1,y_0+1) is chosen the change in D will be: :\begin \Delta D &=& f(x_0+2,y_0+\tfrac 3 2) - f(x_0+1,y_0+\tfrac 1 2) &=& A+B &=& \Delta y - \Delta x \end If the new D is positive then (x_0+2,y_0+1) is chosen, otherwise (x_0+2,y_0). This decision can be generalized by accumulating the error on each subsequent point. All of the derivation for the algorithm is done. One performance issue is the 1/2 factor in the initial value of D. Since all of this is about the sign of the accumulated difference, then everything can be multiplied by 2 with no consequence. This results in an algorithm that uses only integer arithmetic. plotLine(x0, y0, x1, y1) dx = x1 - x0 dy = y1 - y0 D = 2*dy - dx y = y0 for x from x0 to x1 plot(x, y) if D > 0 y = y + 1 D = D - 2*dx end if D = D + 2*dy Running this algorithm for f(x,y) = x-2y+2 from (0,1) to (6,4) yields the following differences with dx=6 and dy=3: D=2*3-6=0 Loop from 0 to 6 * x=0: plot(0, 1), D≤0: D=0+6=6 * x=1: plot(1, 1), D>0: D=6-12=-6, y=1+1=2, D=-6+6=0 * x=2: plot(2, 2), D≤0: D=0+6=6 * x=3: plot(3, 2), D>0: D=6-12=-6, y=2+1=3, D=-6+6=0 * x=4: plot(4, 3), D≤0: D=0+6=6 * x=5: plot(5, 3), D>0: D=6-12=-6, y=3+1=4, D=-6+6=0 * x=6: plot(6, 4), D≤0: D=0+6=6 The result of this plot is shown to the right. The plotting can be viewed by plotting at the intersection of lines (blue circles) or filling in pixel boxes (yellow squares). Regardless, the plotting is the same.


All cases

However, as mentioned above this is only for octant zero, that is lines starting at the origin with a slope between 0 and 1 where x increases by exactly 1 per iteration and y increases by 0 or 1. The algorithm can be extended to cover slopes between 0 and -1 by checking whether y needs to increase or decrease (i.e. dy < 0) plotLineLow(x0, y0, x1, y1) dx = x1 - x0 dy = y1 - y0 yi = 1 if dy < 0 yi = -1 dy = -dy end if D = (2 * dy) - dx y = y0 for x from x0 to x1 plot(x, y) if D > 0 y = y + yi D = D + (2 * (dy - dx)) else D = D + 2*dy end if By switching the x and y axis an implementation for positive or negative steep slopes can be written as plotLineHigh(x0, y0, x1, y1) dx = x1 - x0 dy = y1 - y0 xi = 1 if dx < 0 xi = -1 dx = -dx end if D = (2 * dx) - dy x = x0 for y from y0 to y1 plot(x, y) if D > 0 x = x + xi D = D + (2 * (dx - dy)) else D = D + 2*dx end if A complete solution would need to detect whether x1 > x0 or y1 > y0 and reverse the input coordinates before drawing, thus plotLine(x0, y0, x1, y1) if abs(y1 - y0) < abs(x1 - x0) if x0 > x1 plotLineLow(x1, y1, x0, y0) else plotLineLow(x0, y0, x1, y1) end if else if y0 > y1 plotLineHigh(x1, y1, x0, y0) else plotLineHigh(x0, y0, x1, y1) end if end if In low level implementations which access the video memory directly, it would be typical for the special cases of vertical and horizontal lines to be handled separately as they can be highly optimized. Some versions use Bresenham's principles of integer incremental error to perform all octant line draws, balancing the positive and negative error between the x and y coordinates. Take note that the order is not necessarily guaranteed; in other words, the line may be drawn from (x0, y0) to (x1, y1) or from (x1, y1) to (x0, y0). plotLine(x0, y0, x1, y1) dx = abs(x1 - x0) sx = x0 < x1 ? 1 : -1 dy = -abs(y1 - y0) sy = y0 < y1 ? 1 : -1 error = dx + dy while true plot(x0, y0) if x0

x1 && y0

y1 break e2 = 2 * error if e2 >= dy if x0

x1 break error = error + dy x0 = x0 + sx end if if e2 <= dx if y0

y1 break error = error + dx y0 = y0 + sy end if end while


Similar algorithms

The Bresenham algorithm can be interpreted as slightly modified
digital differential analyzer A digital differential analyzer (DDA), also sometimes called a digital integrating computer, is a digital implementation of a differential analyzer. The integrators in a DDA are implemented as accumulators, with the numeric result converted back ...
(using 0.5 as error threshold instead of 0, which is required for non-overlapping polygon rasterizing). The principle of using an incremental error in place of division operations has other applications in graphics. It is possible to use this technique to calculate the U,V co-ordinates during raster scan of texture mapped polygons. The
voxel In 3D computer graphics, a voxel represents a value on a regular grid in three-dimensional space. As with pixels in a 2D bitmap, voxels themselves do not typically have their position (i.e. coordinates) explicitly encoded with their values. Ins ...
heightmap software-rendering engines seen in some PC games also used this principle. Bresenham also published a Run-Slice (as opposed to the Run-Length) computational algorithm. This method has been represented in a number of US patents: An extension to the algorithm that handles thick lines was created by Alan Murphy at IBM.


See also

* Digital differential analyzer (graphics algorithm), a simple and general method for rasterizing lines and triangles *
Xiaolin Wu's line algorithm 336px, Demonstration of Xiaolin Wu's algorithm. Compression artifacts in the jpeg standard can be made "fairly" with it. Xiaolin Wu's line algorithm is an algorithm for line antialiasing. Antialiasing technique Xiaolin Wu's line algorithm was p ...
, a similarly fast method of drawing lines with
antialiasing Anti-aliasing may refer to any of a number of techniques to combat the problems of aliasing in a sampled signal such as a digital image or digital audio recording. Specific topics in anti-aliasing include: * Anti-aliasing filter, a filter used be ...
*
Midpoint circle algorithm In computer graphics, the midpoint circle algorithm is an algorithm used to determine the points needed for rasterizing a circle. Bresenham's circle algorithm is derived from the midpoint circle algorithm. The algorithm can be generalized to con ...
, a similar algorithm for drawing circles


Notes


References

*
"The Bresenham Line-Drawing Algorithm"
by Colin Flanagan * A very optimized version of the algorithm in C and assembly for use in video games with complete details of its inner workings * , The Beauty of Bresenham's Algorithms


Further reading


Patrick-Gilles Maillot's Thesis
an extension of the Bresenham line drawing algorithm to perform 3D hidden lines removal; also published in MICAD '87 proceedings on CAD/CAM and Computer Graphics, page 591 - .

A.S. Murphy, IBM Technical Disclosure Bulletin, Vol. 20, No. 12, May 1978. rather than ''
hich Ij ( fa, ايج, also Romanized as Īj; also known as Hich and Īch) is a village in Golabar Rural District, in the Central District of Ijrud County, Zanjan Province, Iran Iran, officially the Islamic Republic of Iran, and also ...
' for circle extension use: Technical Report 1964 Jan-27 -11- Circle Algorithm TR-02-286 IBM San Jose Lab or A Linear Algorithm for Incremental Digital Display of Circular Arcs February 1977 Communications of the ACM 20(2):100-106 DOI:10.1145/359423.359432


External links

{{Commons category, Bresenham algorithm
Michael Abrash's Graphics Programming Black Book Special Edition: Chapter 35: Bresenham Is Fast, and Fast Is Good
* ttp://www.pdp8online.com/563/563.shtml Calcomp 563 Incremental Plotter Informationbr>Bresenham Algorithm in several programming languages
– A simple implementation to plot lines, circles, ellipses and Bézier curves Computer graphics algorithms Digital geometry Articles with example pseudocode