Professional Documents
Culture Documents
Line-Drawing Algorithms
ROBERT F. SPROULL
Carnegie-Mellon University
1. INTRODUCTION
M a n y c o m p u t e r g r a p h i c s d e v i c e s use l i n e - d r a w i n g a l g o r i t h m s to a p p r o x i m a t e
s t r a i g h t l i n e s b y d i s p l a y i n g dots t h a t are c o n s t r a i n e d to lie o n a d i s c r e t e grid.
I n c r e m e n t a l p e n p l o t t e r s t h a t m o v e a p e n i n s m a l l s t e p s r e q u i r e s u c h a linegeneration algorithm. Point-plotting C R T displays and electrostatic plotters use
t h e a l g o r i t h m s to a p p r o x i m a t e s t r a i g h t lines. M o r e r e c e n t l y , f r a m e buffer, r a s t e r
s c a n d i s p l a y s use t h e s e a l g o r i t h m s to i d e n t i f y t h e p i c t u r e e l e m e n t s (pixels) t h a t
s h o u l d be i l l u m i n a t e d to d i s p l a y a line.
S i m p l i c i t y a n d s p e e d are t h e k e y d e s i g n c r i t e r i a for l i n e - d r a w i n g a l g o r i t h m s
b e c a u s e t h e c o m p u t a t i o n s are o f t e n i m p l e m e n t e d i n h a r d w a r e i n o r d e r to a c h i e v e
h i g h l i n e - g e n e r a t i o n speeds. I t a p p e a r s t h a t t h e e a r l y p o p u l a r i t y of t h e b i n a r y
r a t e m u l t i p l i e r ( B R M ) was d u e e n t i r e l y to s i m p l i c i t y , for it g e n e r a t e s r a t h e r p o o r
a p p r o x i m a t i o n s to s t r a i g h t lines. T h e digital d i f f e r e n t i a l a n a l y z e r ( D D A ) g e n e r This research was sponsored by the Defense Advanced Research Projects Agency, ARPA Order No.
3597, monitored by the Air Force Avionics Laboratory under Contract F33615-78-C-1551.
The views and conclusions contained in this paper are those of the author and should not be
interpreted as representing the official policies, either expressed or implied, of the Defense Advanced
Research Projects Agency or the U.S. Government.
Author's present address: Xerox Corporation, Palo Alto Research Center, 3333 Coyote Hill Road,
Palo Alto, CA 94304.
Permission to copy without fee all or part of this material is granted provided that the copies are not
made or distributed for direct commercial advantage, the ACM copyright notice and the title of the
publication and its date appear, and notice is given that copying is by permission of the Association
for Computing Machinery. To copy otherwise, or to republish, requires a fee and/or specific
permission.
1982 ACM 0730-0301/82/1000-0259$00.75
ACMTransactionson Graphics,Vol. 1, No. 4, October 1982,Pages 259-273.
260
Robert F. Sproull
ates better approximations to the true line, but requires an iterative loop that
may average almost two cycles to generate each point. An algorithm devised by
J. E. Bresenham [4] dominates the DDA: it generates the optimal line, in the
sense described in Section 2; it requires only integer additions and subtractions;
and it generates one output point for each iteration of the inner loop.
To achieve very high line-generation speeds, an algorithm must compute the
location of several points on a line at once. Such multipoint algorithms have
several applications, chiefly in raster scanned systems that can write more than
one pixel at a time into the image. The investigation of multipoint algorithms
was motivated by the 8 x 8 frame-buffer display [14], which can in one memory
cycle write a square region with 8 pixels on a side located anywhere on the screen.
This paper shows how simple program transformations are used to derive all of
these algorithms, starting from obviously correct algorithms based on simple
analytic geometry. These transformations assure us that the more efficient but
more complex algorithms are correct, because they have been derived by correct
transformations from a correct algorithm. Transformation techniques of different
sorts are used in optimizing compilers [1, 2], are recommended to programmers
for improving their programs [3, 11], and are part of research into automatic
program improvement [7].
2. L I N E - D R A W I N G P R E L I M I N A R I E S
The line-drawing problem is to determine a set of pixel coordinates (x, y), where
x and y are integers, that closely approximates the line from the point (0, 0) to
the point (dx, dy}, for integer values of dx and dy. The assumption that one line
endpoint is at the origin loses no generality because lines with other origins are
simply translations of the line with origin (0, 0). Additionally, lines are restricted
to the first octant: 0 _ dy <_ dx. Again, t h i s assumption loses no generality
because an arbitrary line can be generated by t~ansposing the canonical line or
by reflecting it about one of the principal axes.
The objective of a line-drawing algorithm is to enumerate those pixels that lie
close to the true line, the mathematical line from (0, 0) to (dx, dy). Figure 1
illustrates a typical line, showing with circles the pixels that correspond either to
spots illuminated by a CRT beam on a raster display or to the swath of a plotter
pen. Notice that integral values of coordinates locate pixel centers.
While a line may be displayed using many different pixel configurations, one
configuration is usually preferred. The preference arises because some configurations approximate the true line more closely than others, some appear to have
more uniform pixel density, or brightness, than others, and so on. Many of the
algorithms presented in this paper generate the optimal line, defined as follows:
1. The oPtimal line illuminates exactly one pixel in each vertical column. This
assumption depends on the fact that the line's extent in x exceeds its extent
in y. The purpose of this choice is to limit variations in pixel spacing.
2. Within each column, the pixel illuminated is the one closest to the true line.
To display the optimal line, the line-drawing algorithm must compute, for each
integer xi, the coordinate yi of the pixel that should be illuminated. The coordinate
yt of the true line is simply yt = (dy/dx)x~. Illuminating a pixel centered at yi
ACM Transactions on Graphics, Vol. 1, No. 4, October 1982.
UsingProgramTransformationsto DeriveLine-DrawingAlgorithms
261
y=5j
y=O
x=0
Fig. 1.
x=8
T h e line from (0, 0) to (8, 5). Small dots represent pixel centers. T h e solid line represents
the true line. Circles show t h e pixels t h a t are illuminated to display the optimal line.
f dy
dx
Fig. 2.
Illustration of the relationship between the vertical distance e,, and the perpendicular
distance ep.
3. DERIVATIONOFTHEBRESENHAMALGORITHM
The minimum error formulation of the optimal line leads directly to a simple
algorithm t h a t enumerates all the points on the optimal line, and which can be
expressed in a PASCAL-like language:
A1
var
yt: exactreal;
for xi := 0 to dx do begin
yt :ffi [dy/dx]*xi;
yi := trunc(yt + [1/2]);
display(xi,yi)
end
262
Robert F. Sproull
A2
v a t yt: exactreal; dx, dy, xi, yi: integer;
yt := 0;
for xi := 0 to dx do begin
yi :-- trunc(yt +[1/2]);
* assert yt = (dy/dx)xi *
display(xi,yi);
yl~-- yt + [dy/dx]
end
(1)
A3
var ys: exactreal; dx, dy, xi, yi: integer;
ys := 1/2;
for xi := 0 to dx do begin
yi := tmnc(ys);
* assert y s = (dy/dx)xi + 1/2 = yt + 1/2 *
display(xi,yi);
ys := ys + [dy/dx]
end
(2)
0 <_y~r< 1.
(3)
263
for xi :=0 to dx do b e g i n
S u b s t i t u t i o n o f v a r i a b l e . A l g o r i t h m A4 c a n be t r a n s f o r m e d into t h e B r e s e n h a m
a l g o r i t h m b y replacing t h e use o f y s f with t h a t of a variable r:
r = 2 dy + 2(ysf-
1) d x .
(4)
(5)
1, r is b o u n d e d b y
- - 2 n + l 4-
2~ r
<
2 n+l -
2.
(6)
264
Robert F. Sproull
(1)
y, -
= y~i -
Y~i + Y ~ r -
1),
ffi ~ - Y~f.
1
2
ely
+
dx"
T h e value r is thus linearly related to ev, b u t is offset by 1/2 due to the loop's
initial conditions, offset by ( d y / d x ) because r has already been c o m p u t e d for the
next point along the line when d i s p l a y is called, and scaled by 2 d x to require
only integral values of r.
S u m m a r y . All of the algorithms developed in this section c o m p u t e the same
sequence of points (xi, yi) t h a t approximate the true line. M a t h e m a t i c a l and
program transformations are used to derive efficient implementations. T h e algorithms are usually adapted to draw lines in any o c t a n t by making separately
customized versions for each octant.
4. AN n-STEP ALGORITHM
Before exploring multipoint algorithms, we illustrate the transformation techniques developed in the previous section by deriving an algorithm t h a t takes
horizontal steps of n units in x. Such an algorithm will generate every n t h point
on the line. We start with an obvious variant of AI:
N1
v a t yt: exactreal; dx, dy, xi, yi, n: integer;
for xi := 0 to dx by n do begin
yt := [dy/dx]*xi;
yi := trunc(yt + 1/2);
display(xi,yi)
end
-.
265
end
end
T h e n e x t step is to a p p l y a t r a n s f o r m a t i o n t h a t m a k e s a B r e s e n h a m - l i k e algor i t h m : r = 2n d y + 2 ( y 8 f - 1 - s) d x .
N5
266
Robert F. Sproull
i f sf + [dy/dx] _> 1 t h e n b e g i n
s : = s + 1;
sf := sf + [dy/dx - 1]
e n d else sf := sf + [dy/dx]
end
This prologue has the effect of one division and t h r e e multiplications, all of which
are interleaved in a single loop. F o r f u r t h e r efficiency, the loop m a y be unwound;
for small n, it m a y be u n w o u n d entirely.
5. MULTIPOINT ALGORITHMS
267
yt := [dy/dx]*xi;
yi :-- trunc(yt + [1/2]);
display(xi, yi)
end
parend
b e g i n "N5prologue"
Prologue is identical to N5p, above. *
s:f0;t:=0;
sp := d y - dx;
for i :ffi 0 to n - 1 do b e g i n
assert i(dy/dx) ffi s + (sp + dx - dy)/dx *
t :-- t + [dy + dy];
ACM Transactions on Graphics, Vol. 1, No. 4, October 1982.
268
Robert F Sproull
. .
~)
269
:oo;ooo: :
v a t x: integer;
f o r x := 0 to maxX do p a r b e g i n
Note t h a t the individual pixels of the stroke are written in parallel. In the 8 8
display [14], the DisplayStroke function requires only two m e m o r y cycles (n =
8), one to read the stroke p a t t e r n and one to write it, possibly truncated, at an
arbitrary position in the frame buffer.
This procedure can be incorporated into N5 to yield the complete line-drawing
algorithm Q (the algorithm is shown without the prologue N5p):
Q
v a r dx, dy, xi, ysi, s, t, r: integer;
270
Robert F. Sproull
Fig. 4.
F o u r of t h e e i g h t d i f f e r e n t s t r o k e s w i t h n = 8 a n d a r i s e o f 1.
ysi :----O;
for xi := 0 to dx b y n do begin
i f r ~ 0 t h e n begin
DisplayStroke(xi, ysi, s + 1, min(n - 1, dx - xi))
ysi := ysi + [s + 1];
r := r - [2* d x - t]
e n d else begin
DisplayStroke(xi, ysi, s, min(n - 1, dx - xi))
ysi := ysi + s;
r:=r+t
end
end
271
O
Fig. 5. Lines illustrating gaps and nomnonotonicities t h a t occur w h e n only one kind of stroke is used
to draw a line (n =8). T h e top line ( d x = 23, d y = 18) is drawn with 3 strokes with a rise of 6, which
leave a gap. T h e small dots show the optimal line. T h e b o t t o m line ( d x = 23, d y = 5) shows a
nonmonotonicity when 3 strokes with a rise of 2 are used.
repeatedly to display an entire line, the maximum deviation from the optimal
line may be greater than 1 or the line may have gaps or nonmonotonicities, as
illustrated in Figure 5.
6. CONCLUSION
The early sections of this paper show how simple mathematical and program
transformations can be used to transform an obvious line-drawing method based
on analytic geometry into an efficient and exact algorithm that requires only
integer arithmetic. These methods help persuade us that the algorithm is correct
without recourse to geometric constructions used by Bresenham [4]. The techniques are examples of routine program transformations that should be a commonplace activity in program design and implementation.
The main reason for applying these techniques is to extend line-drawing
algorithms to write several points on a line at once. Although only two multipoint
schemes are explored in Section 5, one can imagine many more. The difficulty of
developing such algorithms is substantially reduced by using program transformations.
APPENDIX
272
R o b e r t F. Sproull
line, and further let a = 2 dy, b = - 2 dx. Define yi to be the y coordinate of the
pixel displayed at x = i; zi is the distance f r o m this pixel to the true line.
T h e B r e s e n h a m algorithm t h a t generates the origin for a stroke will g u a r a n t e e
t h a t I z,il <- - b / 2 . T h e points x = ni + j f o r j = 1 to n are m e m b e r s of one of the
two strokes t h a t r e p r e s e n t a line of slope s / n , where s is the vertical distance
f r o m the origin of the stroke to the origin of the next stroke (i.e., s = yni+n - - y n i ) .
I f these pixels are g e n e r a t e d b y a B r e s e n h a m algorithm aiming at a line of slope
s / n , t h e n we will h a v e Iz, i+j - ( j / n ) ( z n i + , - Zni) ] ~ - b / 2 , for 0 _ j _< n - 1. W e
consider two cases: first, t h a t the expression is positive, and second, t h a t it is
negative.
1. We h a v e Zni+y <-- ( j / n ) ( Z n i + n
- - Z n i ) - - b/2. F r o m the triangle inequality, we
also h a v e ] z,i+, - Znil <-- - b . However, the equality case n e v e r o c c u r s - - i f it did,
the slope of the line would be an integer multiple of 1 / n and the z~i would be zero
for all i. So we now h a v e I Zni+~ -- Z,i] < - b . For 1 <_j <_ n / 2 , this yields Zni+y <
~b.
2. T h e negative case, b y similar a r g u m e n t , gives Zni+i > b for 1 <_j <_ n / 2 .
B o t h cases together I z,i+jl < - b for 1 <_ j <_ n / 2 . B y a similar a r g u m e n t
approaching f r o m the o t h e r side (i.e., x = n i + n, n i + n - 1. . . . ), we obtain
Iz,i-j] < - b for 1 <_ j <_ n / 2 . B o t h forward and b a c k w a r d a p p r o a c h e s t o g e t h e r
give [Zni+jI < - b f o r 1 <_j<_n.
W h e n x = dx, at the endpoint of the line, we m u s t h a v e ]Zdx [ < - b , which,
together with the fact t h a t z m u s t be a multiple of b, forces Zdx = 0. Therefore,
the last point lies exactly on the line.
ACKNOWLEDGMENTS
273
10. JORDAN, B.W., JR., AND BARRETT, R.C. A cell organized raster display for line drawings.
Commun. ACM 17, 2 (Feb. 1974), 70-77.
11. KNUTH, D.E. Structured programming with go to statements. Comput. Surv. 6, 4 (Dec. 1974),
261-301.
12. PITTEWAY, M.L.V. AND GREEN, A.J.R. Bresenham's algorithm with run line coding shortcut.
Comput J. 25, 1 (Feb. 1982), 114-115.
13. REGGIORI, G.B. Digital Computer Transformations for Irregular Line Drawings," Dept. of
Electrical Engineering, New York Univ., Bronx, N.Y., April 1972, pp. 46-61. Available as U.S.
Dept. of Commerce AD-745-015.
14. SPROULL, R.F., SUTHERLAND, I.E., THOMPSON, A., GUPTA, S., AND MINTER, C. The 8 8
display. Tech. Rep. CMU-CS-82-105, Computer Science Dept., Carnegie-Mellon Univ., Pittsburgh,
Pa., Dec. 1981.
Received April 1981; revised April 1982; accepted September 1982