To download NI software, including the products shown below, visit ni.com/downloads.
1. Overview
Bresenham's Line Algorithm calculates the pixels on a line from a given start and end.
2. Description
Bresenham's line algorithm is an 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 standard computer architectures. It is an incremental error algorithm. It is one of the earliest algorithms developed in the field of computer graphics. An extension to the original algorithm may be used for drawing circles.
function line(x0, y0, x1, y1)
real deltax := x1 - x0
real deltay := y1 - y0
real deltaerr := abs(deltay / deltax) // Assume deltax != 0 (line is not vertical),
// note that this division needs to be done in a way that preserves the fractional part
real error := 0.0 // No error at start
int y := y0
for x from x0 to x1
plot(x,y)
error := error + deltaerr
if error = 0.5 then
y := y + 1
error := error - 1.0
The VI takes the X, Y coordinates of the starting and ending points of a line, performs the Bresenham's Line Algorithm and returns both the amount of pixels in that line, as well as the coordinates of each one of those pixels.
3. Requirements
NI LabVIEW Base Development System 2012 (or compatible).
4. Steps to Implement or Execute Code
5. Additional Information or References
**The code for this example has been edited to meet the new Community Example Style Guidelines.**
Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.