Jump to content

Line drawing algorithm: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
A naive line-drawing algorithm: dX => dx, dY => dy to make consistent. dx <=> dy. You had this backwards. Slope is dy/dx ... just plug in the end points x2 and x1 to see.
Link to the graphics anti-aliasing topic, instead of the DAB. Link just once per MOS
Line 2: Line 2:
[[File:Line scan-conversion.svg|thumb|upright=1.0|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.]]
[[File:Line scan-conversion.svg|thumb|upright=1.0|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.]]


A '''line drawing algorithm''' is a graphical [[algorithm]] for approximating a line segment on discrete graphical media. On discrete media, such as [[pixel]]-based [[computer display|displays]] and [[computer printer|printers]], line drawing requires such an approximation (in nontrivial cases). Basic algorithms rasterize lines in one color. A better representation with multiple color gradations requires an advanced process, [[anti-aliasing]].
A '''line drawing algorithm''' is a graphical [[algorithm]] for approximating a line segment on discrete graphical media. On discrete media, such as [[pixel]]-based [[computer display|displays]] and [[computer printer|printers]], line drawing requires such 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, [[oscilloscope]]s use natural phenomena to draw lines and curves.
On continuous media, by contrast, no algorithm is necessary to draw a line. For example, [[oscilloscope]]s use natural phenomena to draw lines and curves.
Line 32: Line 32:
*[[Digital Differential Analyzer (graphics algorithm)]] — Similar to the naive line-drawing algorithm, with minor variations.
*[[Digital Differential Analyzer (graphics algorithm)]] — Similar to the naive line-drawing algorithm, with minor variations.
*[[Bresenham's line algorithm]] — optimized to use only additions (i.e. no divisions or multiplications); it also avoids floating-point computations.
*[[Bresenham's line algorithm]] — optimized to use only additions (i.e. no divisions or multiplications); it also avoids floating-point computations.
*[[Xiaolin Wu's line algorithm]] — can perform [[spatial anti-aliasing]], appears "ropey" from brightness varying along the length of the line
*[[Xiaolin Wu's line algorithm]] — can perform spatial anti-aliasing, appears "ropey" from brightness varying along the length of the line
*[[Gupta-Sproull algorithm]]
*[[Gupta-Sproull algorithm]]



Revision as of 10:29, 2 May 2016

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.

A line drawing algorithm is a graphical algorithm for approximating a line segment on discrete graphical media. On discrete media, such as pixel-based displays and printers, line drawing requires such 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, oscilloscopes use natural phenomena to draw lines and curves.

The Cartesian slope-intercept equation for a straight line is With m representing the slope of the line and b as the y intercept. Given that the two endpoints of the line segment are specified at positions and . we can determine values for the slope m and y intercept b with the following calculations, so, .

A naive line-drawing algorithm

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

dx = x2 - x1
dy = y2 - y1
for x from x1 to x2 {
  y = y1 + dy * (x - x1) / dx
  plot(x, y)
}

It is assumed 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 lots of gaps, and in the limiting case of , only a single point is plotted.

The naïve 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.

List of line drawing algorithms

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

The following is a partial list of line drawing algorithms:

References

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