Está en la página 1de 3

A N EFFICIENT LINE ALGORITHM

A . T. M. ShafiquI Khalid’ M. KaykobadZ


‘Ascent Solutions Inc.
9009 Miamisburg Springboro Pike
Dayton, OH45342, USA
Khalid@asizip.com
2Dept. of Computer Science & Engineering
Bangladesh University of Engineering & Technology
Dhaka-1000, BANGLADESH

ABSTRACT devices may not appear to be perfectly straight, the line ac-
curacy has been claimed to improve in proportion to the dis-
We present a new algorithm for drawing lines in a raster play resolution. One of the most important characteristics
device in which a suitable data structure has been chosen to of this algorithm is that it requires lesser number of compar-
avoid comparisons that are required, for example, in Bre- isons thanthat of Bresenham. Some algorithm speed up the
senham’s algorithm. Experimental results as well as clock line drawing process by calculating few pixels using symme-
cycles calculated theoretically suggest that this new algo- try property of line and double step technique[6]. Proposed
rithm outperforms the ones currently existing in the liter- algorithm can effectively used for further improvement of
ature in terms of computational time. Our experimental those algorithm as it improve basic pixel calculating prc-
results also suggest that quality of the line does not deteri- cess.
orate even when high resolution raster devices are used.
2. THE NEW ALGORITHM
1. INTRODUCTION As in the case of Bresenham’s algorithm we assume that
Line drawing is one of the most fundamental activities in the line to be drawn always makes a non-negative angle not
computer graphics. There are many M e r e n t line drawing exceeding 45 degrees with the x-axis. Other cases can be
algorithms used in computer graphics. Application of ineffi- dealt with using routine transformations. The main feature
cient algorithms may cause drawing to require unacceptably of the algorithm is that it does not require comparisons be-
large amount of time thus making the graphics presentation fore putting a pixel on as is done in Bresenham’s and other
boring. This is why algorithms used in computer graphics algorithms. This results in a drastic reduction of compu-
must be computationally very efficient. As a result com- tational time. This has become possible observing the fact
puter graphics algorithms are quite often found to avoid, that even raster devices having very high resolutions do not
for example, floating point operations or more costly divi- require calculations based on 4-byte words. As in other
sion and multiplication operations as in Bresenham’s line algorithms x-values will be incremented by one whereas y-
algorithm. As a line is drawn by lighting only a finite num- values will be represented as an integer, the leftmost 2 bytes
ber of pixels with integer coordinates, it is not possible to of which will contain the y-coordinate of the pixel and the
produce a theoretical line exactly in a raster device. In order rightmost 2 bytes wiU represent the fractional increment
that a line drawn on a raster device simulates a theoretical part, which when sufficiently accumulated will cause the
line as closely as possible, a set of pixels, which represent value of the leftmost part automatically changed, and will
the real line as closely as possible, are only switched on. always contain the right value for scan conversion without
Assuming that line to be drawn makes an angle between 0 requiring any comparison. In order that the computed y co-
and 45 degrees with the x-axis, it can be easily said that ordinate is represented by the closest pixel a half is added
in between successive pixels only two kinds of movements to the y-value so that integer portion becomes closest to
will be available: sideways- keeping y-value fixed x-value is the computed y-value. The pseudocode of the algorithm is
increased or diagonal - both x and y values are increased given in the following:
by 1. This is essentially what is done both in Bresenham’s
or recently introduced recursive bisection algorithms. Procedure N e w L i n e ( x 1 , y1, 22, y2)

Existing algorithms for line drawing can be classified as // X I , y l , 5 2 , y2 are respectively coordinates of the first
incremental, nonincremental, or they may differ in the order and second end points of the line which makes a non-
of setting the pixels that represent the line [1,3,4]. Borges[2] negative angle not larger than 45 degrees with the x-axis.//
considered the problem of line drawing, where coordinates
of endpoints need not necessarily be integers, and developed inc = (Leftshift(y2-yl ,l6))/(x2-x1)
some rules that can rescue from round-off errors. Rankin[5] yl = 2 k - 1
has developed a recursive bisection line algorithm based on Y h = Y1
the fractal definition of the line, and showed that recur- //variable y is a 2k bit number partitioned i n t o
sive nature of the algorithm results in fewer decisions being / / t w o ports
made in comparison with other algorithms. This resulted // yr the lower k bits and yh t h e upper k bits.
in this algorithm’s faster execution particularly with lines f o r x = X I to 2 2 do
near to horizontal, diagonal or vertical. Although the algo- putpixel( 2 , y h )
rithm does not produce a palindrome and on low resolution y = y -k inc

$10.00 0 1997 IEEE


0-7803-3636-4197 1280
endfor
end NevLine

In order t o compare this algorithm with that of Bresen-


ham’s we present the pseudocode of Bresenham’s algorithm
in the following:
Procedure Bresenham(x1, y1, XZ, yz )

//XI, yl, x2, yz are respectively coordinates of the first


and second end points of the line which makes a non-
negative angle not larger than 4 5 degrees with the x-axis.//
1 dx=
x2

100

400
500
600
700
- XI
proposed
algorithm

I 3.6
~ :bP3
13.8
17.2
20.6
24.1
27.5
30.9
I 400
I 5.76
11 4
1171
22 7
28 1
33 6
39 5
44 5
51.2
Brezenham algorithm

600
dy = ~2 - YI
700
5.98 6.04 6.15
11.7 11.9 12.1
17.6 17.9 18.2
23.4 23.8 24.2
29.3 29.8 30.3
34.9 35.6 36.1
40.9 41.7 42.5
46.0 46.8 47.6
52.9 53.8
800 900
6.26
12.4
18.5
24.6
30.9
36.8
43.2

-
dx = ~2 XI 1000 34.3 56 3 58.8 60.0 -
-
d y = Y Z YI
p = Leftshift(dy,f) From the above table it is clear that the Bresenham al-
t=p-dx gorithm takes on the average 50% to 80% more computa-
q=t-dx tional time than the NewLine algorithm, where the first
Y = Y1 figure corresponds to the case of lines with slope 0.1, and
for x = X I to 2 2 do the last figure to the case of lines with slope 0.9. We have
putpixel(x, y) also checked the computational time of the recursive bisec-
if t 0 then tion algorithm which takes more time than the Bresenham
y = y f l algorithm for lines with slope closer to 45 degrees, but for
t = t + q lines with angles closer t o 0 degree its computation time on
else the average is marginally better, approximately lo%, than
t = t f p the Bresenham’s. This shows that the NewLine algorithm
endif is faster than the recursive bisection algorithm also. As our
endfor algorithm improve basic conversion(pixe1 position calcula-
end Bresenham tion) it is expected that it will improve all the algorithms
that require those type calculation.
It can be easily seen from the Bresenham’s algorithm that
in the for-loop in addition to putpixel it requires one com- 4. CONCLUSION
parison (which also necessitates one jump operation), and From the above analysis it i s clear that performance of the
one to two additions. On the other hand in the NewLine al- Bresenham algorithm depends on the values of dy, and it
gorithm for-loop contains only a single addition operation in takes time 50% to 80% more than that of the NewLine algo-
addition to the putpixel operation. Bresenham’s algorithm, rithm. Assembly code for the NewLine is smaller in length
in addition, requires 4 addition operations, one assignment than that of the Bresenham’s code. So memory require-
and one shift operations, where as our algorithm has an ments for the NewLine is allso less. Hardware implementa-
overhead of a single integer division, two assignment and tion for the NewLine will be much cheaper than the other
a shift operations. Considering the number of clock cycles algorithms. Only drawback of the proposed algorithm is
required by each of these primitive operations time com- in its single division operation. Since higher precision di-
plexity of these two algorithms has been analyzed in the vision operation is not required here, divider can be imple-
following section. mented by a very simple hardware. There are some other
3. PERFORMANCE ANALYSIS algorithms[5-61 that apply the technique used by Braxen-
ham but calculate comparative fewer pixels. Our proposed
Analyzing the assembly codes(1NTEL 386-DX) for both the technique can efficiently be used for the improvement of
algorithm we find clock cycle requirement for both the al- those algorithms.
gorithm. Expression for proposed algorithm is 27dx+59 for
Brazenham 46dx+5dy+62 clock cycle to calculate all pixel
position of a line having dx horizontal and dy vertical move- REFERENCES
ment.
[l] J.E. Bresenham. a Algorithm for computer control of
The following table shows time requirements for both the a digital plotter.” IBM Systems Journal, vol. 4, no. 1,
pp. 25-30, 1965.
algorithms while implementing in MASM assembly codes.
Algorithms have been applied on each set of data 65536 [2] Rudolf Borges. Line algorithms for raster displays
times excluding putpixel overhead. If putpixel overhead is rescued from round-off Errors”. Computers and Graph-
counted then some constant time will be added t o both the ics, vol. 15, no. 2, pp. 155-160, 1991.
cases. From the experimental results and the complexity [3] C.M.A. Castle and M.L.V. Pitteway. An applica-
analysis it can be concluded that the Newline algorithm is
tion of Euclid’s algorithm to drawing straight lines,
always better Bresenham algorithm in terms of computa- In Fundamental Algornthms for Computer Graphics”.
tion time. NATO AS1 series F: Computers and System Sciences,
Springer-Verlag, 1985.
In the table second column shows time requirement for
the NewLine algorithm which is independent of dy. Other [4] W.M. Newman and R.F. Sproull. ‘‘ Principles for in-
columns show time requirements for Bresenham algorithm teractive computer graphics”. 3rd edn., McGraw-]Kill,
that depends on dy for a given value of dx. NEW, pp. 22-27, 1981.

1281
[5] John R. Rankin. “ Recursive bisection line algorithm,
Computers and Graphics”. vol. 15, no. 1, pp. 1-8, 1991.
[6] I. G. Rokne, Brian Wyvile, Xiaolin Wu. Fast line
scan conversion”. ACM transaction on grapgics vol. 9,
no. 4, pp. 377-388, 1990.

1282

También podría gustarte