Lably
Skip to content

Line drawing algorithm

  • 6 min read
  • 4 chapters

Related on Lably

Through Algorithm, Graphics and Pixel

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

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.

Simple Methods

The simplest method of drawing a line involves directly calculating pixel positions from a line equation. Given a starting point (x1,y1) and an end point (x2,y2), points on the line fulfill the equation y=m(x−x1)+y1, with m=ΔyΔx=y2−y1x2−x1 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 <b id="mwKQ">for</b> x <b id="mwKg">from</b> x1 <b id="mwKw">to</b> x2 <b id="mwLA">do</b> 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:

Therefore, it is enough to simply start at the point (x1,y1) 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 m=ΔyΔx=y2−y1x2−x1, which is still necessary at the beginning.

These algorithm works just fine when dx≥dy (i.e., slope is less than or equal to 1), but if dx<dy (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.

Issues

In certain situations, single color line drawing algorithms run into issues:

Inconsistent brightness

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

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.

Sign in to keep reading

3 more chapters are free with a Lably account.

More in Computing and communication