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. 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 representing the slope of the line and 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 and y-intercept with the following calculations: so,.

List of line drawing algorithms

The following is a partial list of line drawing algorithms:
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 × / dx
plot
It is here that the points have already been ordered so that.
This algorithm works just fine when , but if , the line becomes quite sparse with lots of gaps, and in the limiting case of, a division by zero exception will occur.
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.