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). Basic algorithms rasterize lines in one color. A better representation with multiple color gradations requires an advanced process, spatial anti-aliasing.
On continuous media, by contrast, no algorithm is necessary to draw a line. For example, cathode-ray oscilloscopes use analog phenomena to draw lines and curves.
Single color line drawing algorithms involve drawing lines in a single foreground color onto a background. They are well-suited for usage with monochromatic displays.
The starting point and end point of the desired line are usually given in integer coordinates, so that they lie directly on the points considered by the algorithm. Because of this, most algorithms are formulated only for such starting points and end points.
The simplest method of drawing a line involves directly calculating pixel positions from a line equation. Given a starting point
(x1,y1)
(x2,y2)
y=m(x-x1)+y1
stylem=
\Deltay | |
\Deltax |
=
y2-y1 | |
x2-x1 |
dx = x2 − x1 dy = y2 − y1 m = dy/dx for x from x1 to x2 do y = m × (x − x1) + y1 plot(x, y)
Here, the points have already been ordered so that
x2>x1
This algorithm is unnecessarily slow because the loop involves a multiplication, which is significantly slower than addition or subtraction on most devices. A faster method can be achieved by viewing the Difference between two consecutive steps:
yi+1-yi | =(m(xi+1-x1)+y1)-(m(xi-x1)+y1) | |
=m(xi+1-xi) | ||
=m |
Therefore, it is enough to simply start at the point
(x1,y1)
y
m
Because rounding
y
y+0.5
m
y
stylem=
\Deltay | |
\Deltax |
=
y2-y1 | |
x2-x1 |
These algorithm works just fine when
dx\geqdy
dx<dy
dx=0
In certain situations, single color line drawing algorithms run into issues:
When drawing lines of the same length with differing slopes, different numbers of pixels are drawn. This leads to steeper lines being made up of fewer pixels than flatter lines of the same length, which leads to the steeper line appearing brighter than the flat line. This problem is unavoidable on monochromatic devices.
Clipping is an operation that limits rasterisation to a limited, usually rectangular, area. This is done by moving the start- and end points of the given line to the borders of this area if they lie outside of it. Generally, this leads to the coordinates of these points no longer being integer numbers. If these coordinates are simply rounded, the resulting line will have a different slope than intended. For this issue to be avoided, additional tests are necessary after clipping.
The biggest issue of single color line drawing algorithms is that they lead to lines with a rough, jagged appearance. On devices capable of displaying multiple levels of brightness, this issue can be avoided through antialiasing. For this, lines are usually viewed in a two-dimensional form, generally as a rectangle with a desired thickness. To draw these lines, points lying near this rectangle have to be considered.
The Gupta-Sproull algorithm is based on Bresenham's line algorithm but adds antialiasing.
An optimized variant of the Gupta-Sproull algorithm can be written in pseudocode as follows:
DrawLine(x1, x2, y1, y2)
The IntensifyPixels(x,y,r) function takes a radial line transformation and sets the intensity of the pixel (x,y) with the value of a cubic polynomial that depends on the pixel's distance r from the line.
Line drawing algorithms can be made more efficient through approximate methods, through usage of direct hardware implementations, and through parallelization. Such optimizations become necessary when rendering a large number of lines in real time.
Boyer and Bourdin introduced an approximation algorithm that colors pixels lying directly under the ideal line.[1] A line rendered in this way exhibits some special properties that may be taken advantage of. For example, in cases like this, sections of the line are periodical. This results in an algorithm which is significantly faster than precise variants, especially for longer lines. A worsening in quality is only visible on lines with very low steepness.
A simple way to parallelize single-color line rasterization is to let multiple line-drawing algorithms draw offset pixels of a certain distance from each other.[2] Another method involves dividing the line into multiple sections of approximately equal length, which are then assigned to different processors for rasterization. The main problem is finding the correct start points and end points of these sections.
Algorithms for massively parallel processor architectures with thousands of processors also exist. In these, every pixel out of a grid of pixels is assigned to a single processor, which then decides whether the given pixel should be colored or not.[3]
Special memory hierarchies have been developed to accelerate memory access during rasterization. These may, for example, divide memory into multiple cells, which then each render a section of the line independently.[4] Rasterization involving Antialiasing can be supported by dedicated Hardware as well.[5]
Lines may not only be drawn 8-connected, but also 4-connected, meaning that only horizontal and vertical steps are allowed, while diagonal steps are prohibited. Given a raster of square pixels, this leads to every square containing a part of the line being colored. A generalization of 4-connected line drawing methods to three dimensions is used when dealing with voxel grids, for example in optimized ray tracing, where it can determine the voxels that a given ray crosses.
Line drawing algorithms distribute diagonal steps approximately evenly. Thus, line drawing algorithms may also be used to evenly distribute points with integer coordinates in a given interval.[6] Possible applications of this method include linear interpolation or downsampling in signal processing. There are also parallels to the Euclidean algorithm, as well as Farey sequences and a number of related mathematical constructs.[7]