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). 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 electronics to draw lines and curves. Before the advent of fast CPUs, such monitors were used in advanced application CAD/CAM domains such as car design, using wireframe drawings. These systems only needed a small list of (x,y) vectors from the computer to immediately draw a line image. For a visualisation on a display that only accepts intensities of pixels, a digital algorithm needed to be invented.
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 electronics to draw lines and curves. Before the advent of fast CPUs, such monitors were used in advanced application CAD/CAM domains such as car design, using wireframe drawings. These systems only needed a small list of (x,y) vectors from the computer to immediately draw a line image. For a visualisation on a display that only accepts intensities of pixels, a digital algorithm needed to be invented. The simplest method of drawing a line involves directly calculating pixel positions from a line equation. Given a starting point (x_1, y_1) and an end point (x_2, y_2) , points on the line fulfill the equation y = m(x-x_1) + y_1 , with \textstyle m = \frac{\Delta y}{\Delta x} = \frac{y_2-y_1}{x_2-x_1} being the slope of the line. The line can then be drawn by evaluating this equation via a simple loop, as shown in the following pseudocode: 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 x_2 > x_1 . 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: Therefore, it is enough to simply start at the point (x_1, y_1) and then increase y by m once on every iteration of the loop. This algorithm is known as a Digital differential analyzer. Because rounding y to the nearest whole number is equivalent to rounding y+0.5 down, rounding can be avoided by using an additional control variable that is initialized with the value 0.5. m is added to this variable on every iteration. Then, if this variable exceeds 1.0, y is incremented by 1 and the control variable is decremented by 1. This allows the algorithm to avoid rounding and only use integer operations. However, for short lines, this faster loop does not make up for the expensive division \textstyle m = \frac{\Delta y}{\Delta x} = \frac{y_2-y_1}{x_2-x_1} , which is still necessary at the beginning. These algorithm works just fine when dx \geq dy (i.e., slope is less than or equal to 1), but if dx (i.e., slope greater than 1), the line becomes quite sparse with many gaps, and in the limiting case of dx = 0 , a division by zero exception will occur. x = x1; y = y1; dx = x2 − x1; dy = y2 − y1; d = 2 * dy − dx; // discriminator // Euclidean distance of point (x,y) from line (signed) D = 0; // Euclidean distance between points (x1, y1) and (x2, y2) length = sqrt(dx * dx + dy * dy); sin = dy / length; cos = dx / length; while (x <= x2) { IntensifyPixels(x, y − 1, D + cos); IntensifyPixels(x, y, D); IntensifyPixels(x, y + 1, D − cos); x = x + 1 if (d <= 0) { D = D + sin; d = d + 2 * dy; } else { D = D + sin − cos; d = d + 2 * (dy − dx); y = y + 1; } } } 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.
References
No reference rows published for this article.
Cite this article
MLA
"Line drawing algorithm." encyclopegia.org, 2026, https://encyclopegia.org/wiki/line-drawing-algorithm. Accessed 24 November 2026.
Chicago
"Line drawing algorithm." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/line-drawing-algorithm.
APA
encyclopegia.org. (2026). Line drawing algorithm. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/line-drawing-algorithm
Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON