Jump to content

Line drawing algorithm

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 213.233.110.149 (talk) at 17:59, 22 November 2022 (A naive line-drawing algorithm: Fixed typo). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Two rasterized lines. The colored pixels are shown as circles. Above: monochrome screening; below: Gupta-Sproull anti-aliasing; the ideal line is considered here as a surface.

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.

List of line drawing algorithms

Lines using Xiaolin Wu's algorithm, showing "ropey" appearance.

The following is a partial list of line drawing algorithms:

A naive line-drawing algorithm

The simplest method of screening is the direct drawing of the equation defining the line.

dx = x2x1
dy = y2y1

for x from x1 to x2 do
    y = y1 + dy × (xx1) / dx
    plot(x, y)

It is here that the points have already been ordered so that . This algorithm works just fine when (i.e., slope is less than or equal to 1), but if (i.e., slope greater than 1), the line becomes quite sparse with many gaps, and in the limiting case of , a division by zero exception will occur.

The naive line drawing algorithm is inefficient and thus, slow on a digital computer. Its inefficiency stems from the number of operations and the use of floating-point calculations. Line drawing algorithms such as Bresenham's or Wu's are preferred instead.

References

  • Fundamentals of Computer Graphics, 2nd Edition, A.K. Peters by Peter Shirley