Sunteți pe pagina 1din 5

Fast Algorithm for Line Rasterization by Using Slope 1

Hua Zhang*,† Changqian Zhu* Jun Yang*


hzhang@caep.ac.cn cqzhu@home.swjtu.edu.cn yangj@mail.lzjtu.cn
*
Dept. of Computer and Communication Engineering, Southwest Jiaotong University

Institute of Computer Applications, China Academy of Engineering Physics

Abstract y

In this paper, a fast algorithm for line rasterization (0, 1) ? (1, 1)


is presented by using approximate pixels. Slope 1 is
used to decide which pixel is selected instead of using
slope 1/2. Compared with Bresenham’s middle point
algorithm, three multiplications are reduced in each
line raster process. Moreover, experiment shows that ? (1, 1/2)
at least 6.315 percent of time is saved, and there are
almost the same results for viewer compared with
those of Bresenham’s middle point algorithm.

?
(0, 0) (1, 0) x
1. Introduction
Figure 1. Basis of Bresenham’s algorithm.
Line rasterization is a basic subject of digital
plotter, numerical control, and computer graphics.
Since the upper fast algorithms just emphasize on
There are many papers, which discussed the efficiency
algorithm itself, How about line in practical usage is?
and implementation of line rasterization, such as [1],
In a recent paper, Chen, Wang and Bresenham [8] give
[3], [4], [5], [7], [8], [9], [10] and [11]. Among these
out detailed and clear results about this question after
papers, Bresenham’s middle point algorithm [1] is the
studying many current line drawing packages. One of
most classical and widely used algorithm, which has
the most important results is that 87.79 percent lines
the advantages of raster precision and calculation
are less than or equal to 17 pixels. So, in this case, the
efficiency, and only integer operation is needed.
fast algorithm based on the longer length of strait line
Although Bresenham’s middle algorithm is fast
may not have more advantage than we expect before.
enough, numerous applications require higher speed,
In this paper, a faster algorithm based on Bresenham’s
such as real simulation, visualization of large data sets,
middle algorithm without line length limits is
and rendering more complex objects. Many algorithms
presented.
have discussed this problem. There are two kinds of
Now, let us to consider this question: why should
algorithms for fasting the Bresenham’s middle
we raster the line? One of the important reasons is to
algorithm. One use hardware, such as [5], [7], [9], and
show the natural objects on screen. Surely, when
[10]. Wu et al. [5] present a fast algorithm for line
considering in digital plotter and numerical control, the
rasterization based on double-step technology. Gill [7]
precision is the most important factor. This paper just
presents his fast algorithm which takes advantage of
emphasize on the drawing lines on screen display. If a
N-step technology. Earnshaw [9] and Castle et al. [10]
final result on scene can not be distinguished the
take advantage of the repeated patterns that algorithm
difference between two images (rendered with
generates strait lines. On the other hand, Rokne [3] and
different method) by eyes, approximate method could
Wright [4] present parallelization method for fasting
be a selection if it has some advantages, such as
the Bresenham’s middle algorithm.
calculation efficiency(Bresenham’s middle algorithm

0-7803-8963-8/05/$20.00 ©2005 IEEE 508


Table 1. Breseham’s algorithm Start

void line(int x0,int y0, int xn,int yn) no


swapd(&x0,&xn)
{ xn < x0
swapd(&y0,&yn)
int dx,dy,inCreE,inCreNE,d,x,y,flag=0;
yes
if (xn<x0) { swapd(&x0,&xn);swapd(&y0,&yn);}; no y0=-y0
if (yn<y0) { y0=-y0,yn=-yn,flag=10;}; yn < y0 yn=-yn
dy=yn-y0; dx=xn-x0; flag=10

if (dx<dy){ yes
swapd(&x0,&y0);swapd(&xn,&yn);swapd(&dy,&dx);
dy = yn - y0
flag++; dx = xn - x0
}
swapd(&x0,&xn)
x=x0;y=y0;d=2*dy-dx;inCreE=2*dy;inCreNE=2*(dy-dx); yes swapd(&y0,&yn)
dx < dy
swapd(&dx,&dy)
flag ++
while(x<xn+1){
no
writepixel(x,y,flag);
x = x0
x++; y = y0
if (d<=0) d+=inCreE; d = dy - dx
inCreE = dy
else {y++,d+=inCreNE;} inCreNE = dy - dx
}
}
no
Where d is error for representing the line, inCreE is the x < xn + 1
error in east direction, inCreNE is the error in northeast
direction, and swapd is a function of exchanging the yes

values of two variables.


writepixel(x,y,flag)
x ++
Table 2. Our algorithm.

yes
//Assume two end coordinates are inter, as supposed by Bresenham. d<0 d += inCreE
void ourline(int x0, int y0, int xn, int yn)
no
{
int dx,dy,inCreE,inCreNE,d,x,y,flag=0; y ++
if (xn<x0) { swapd(&x0,&xn);swapd(&y0,&yn);}; d += inCreNE

if (yn<y0) { y0=-y0,yn=-yn,flag=10;};

dy=yn-y0; dx=xn-x0; End


if (dx<dy){
swapd(&x0,&y0);swapd(&xn,&yn);swapd(&dy,&dx); Figure 2. Flowchart of our algorithm.
flag++;
} is also an approximate algorithm for 2D raster display,
x=x0;y=y0;d=dy-dx;inCreE=dy;inCreNE=dy-dx; although it has the smallest error to represent line).
while(x<xn+1){ In this paper, we always assume the slope of line
writepixel(x,y,flag); has the range of 0 to 1. Other lines could be obtained
x++; by reflecting along principal axes. In fig. 1, for
if (d<0) d+=inCreE; Bresenham’s algorithm, if the slope of line is less than
else {y++,d+=inCreNE;} or equal to ½, pixel (1, 0) is selected. If the slope of the
} line is large than ½, pixel (1, 1) is selected. For the
} next pixel, the slope of the line is added to error, which
is initialized to –1/2. The sign of error is used to decide
which pixel is selected from northeast or east pixel. If
Where d is error for representing the line, inCreE is the
the sign of error is positive, the northeast pixel is
error in east direction, inCreNE is the error in northeast
direction, and swapd is a function of exchanging the selected. If the sign of error is negative or equal to
values of two variables. zero, the east pixel is selected. The C program [6] for

509
Bresenham’s algorithm is described in table 1. In table
1, error d, inCreE, and increNE are calculated before
line raster inner loop. The variable flag is used to
indicate which octant the line lies. Function swapd is
used to exchange the coordinates of two end points in
order to make the longer edge as the x direction axis,
since line rasterization precision need to be considered
when the slope of line is larger than 1, and get more
pixels to represent line [1].

2. Algorithm
In fig. 1, if the slope of the line is less than 1, pixel
(1, 0) is selected to represent the line. If the slope of
the line is equal to 1, (1, 1) is selected to represent the
line. Because it is desirable to check only the sign of
error term as used in Bresenham’s algorithm, error is
initialized to -1 instead of -1/2 in Bresenham’s Figure 3. The ratio between our time with
algorithm. Let d be the error term, inCreE be error Bresenham’s time in percent.
term when the east pixel is selected, and inCreNE be
Where horizontal axis is the y coordinates of the
the error term when the northeast error term is
second end points, and the vertical axis is the time
selected. Now the error term d at the first pixel from ratio.
end point could be represented as following:

d = -1 +deltay/deltax (1)
3. Comparisons

Since deltax is larger than zero, multiplying both 3.1. Efficiency


sides of (1) by deltax, we could obtain:
When start to raster a line, we also assume the
d x deltax = deltay - deltax coordinates of two end points are integers, as supposed
and used in many line rasterization algorithms,
Because d has same sign as that of d x deltax and including in software and hardware, such as in [4], [5],
only the sign of error term is needed to raster the line, [7], and [10]. If the coordinates are not integers, round
let d = d x deltax. If the east point is selected, another computation could be used [1].
error deltay/deltax is introduced, named as inCreE. If Compared with table 1, table 2 (see bold code)
the northeast pixel is selected, error deltay/deltax - 1 is decreased three multiplication computations in each
introduced, named as inCreNE. So, variables inCreE line rasterization process, and need not to consider the
line length limit. That is to say, in whatever the length
and inCreNE could be obtained as following:
of line, three multiplications are saved for each line
rasterization process.
inCreE = deltay
Since Chen et al. [8] present a statistical result that
inCreNE = deltay - deltax
87.79 percent of lines are shorter than or equal to 17
pixels, we take advantage of this result without losing
The C code of this algorithm could be represented
generality, and select 15 pixel length in x directional
in Table 2. Note that the bold code is different from
scan line. With regard to slope, we test slope from 0 to
those of table 1. Compared with Bresenham’s
1 in first octant( using this method, a result about how
algorithm (see table 1.), three multiplications are
the slope affect the efficiency could be gotten). Line in
reduced for each line rasterization process; and the
the other octants could be obtained by being reflected
logical comparison calculation is changed from <= to
along with principal axes.
<. Figure 2 is a flowchart of our algorithm. Since there
Our testing environment is a personal computer,
are only three multiplications are saved in initializing
which includes Intel PIII 500 CPU, RAM 384M, and
step, not in inner loop, when rastering a line compared
Operating System is Windows 2000 professional.
with Bresenham’s algorithm, are there any merits for
From table 3, we can note that the maximum ratio
our algorithm?

510
is 93.685 percent. That is to say, 6.315 percent of time Table 3. Efficiency Comparison
is saved with our algorithm when rastering line from
point (0, 0) to point (15, 0). The minimum ratio is
88.339 percent (11.661 percent of time is saved with
our algorithm when rastering the line from point (0, 0) Bresenham's Our time / Breseham
to point (15, 9)). An explicit curve about changing Our Algorithm
(x0,y0,xn,yn) Algorithm time
trend is showed in fig. 3. The zigzag figure is affected (µs)
(µs) %
by the inner loop computation of a whole line
rasterization process with regard to different slope. So,
our algorithm is faster than Bresenham’s middle (0,0,15,0) 476.517 446.425 93.685
algorithm from slope 0 to 1 in many(exactly 87.79
percent) actual rasterizations. (0,0,15,1) 502.578 468.495 93.218
(0,0,15,2) 528.559 490.565 92.811
3.2. Precision
(0,0,15,3) 506.489 472.406 93.270
Let us see fig. 1, middle scan line is the decision (0,0,15,4) 484.419 446.705 92.214
variable for Bresenham’s middle algorithm, and the
max error in vertical direction is 0.5 dot distance(Note (0,0,15,5) 510.400 472.686 92.610
that the line can be in any directions. That is to say, the (0,0,15,6) 560.127 520.457 92.917
error measured by vertical scan line, not by the right
line from pixel centre to the line in theory). However, (0,0,15,7) 540.571 494.476 91.472
in our algorithm, the max error in vertical directions is
(0,0,15,8) 492.521 446.705 90.697
one dot distance. So, our algorithm has two times error
as that of Bresenham’s middle algorithm. Surely, in (0,0,15,9) 582.197 514.311 88.339
precision case, our algorithm is not better than that of
(0,0,15,10) 552.584 492.531 89.132
Bresenham’s, and even worse compared with
Bresenham’s middle algorithm. Our algorithm can not (0,0,15,11) 594.489 544.203 91.541
be used in the areas requiring high interpolation
precision, such as digital plotter and numerical control. (0,0,15,12) 556.495 496.711 89.257
How about computer displaying is when using our (0,0,15,13) 574.375 522.413 90.953
algorithm? Is this the same result as that of areas
requiring high precision? The following section may (0,0,15,14) 528.559 472.965 89.481
give some hints for the answers. (0,0,15,15) 530.514 470.425 88.673
Where x0, y0, xn, and yn are end coordinates of two
3.3. Visual effect end points. This table is based on the following testing
environment: Intel PIII 500M, RAM 384M, and Windows
Since triangle is the current popular element used 2000 Professional operating system.
to approximate to facet objects, two triangles are used
in this paper to show the difference of vision by using A method of line rasterization is presented in this
our algorithm and Bresenham’s middle algorithm. paper. Compared with Bresenham’s middle point
Fig. 4 is a triangle rendered by using Bresenham’s algorithm, our algorithm has following advantages:
middle point algorithm. Fig. 5, which is rendered by three multiplications are reduced for each line
our algorithm, is a triangle with same coordinates of rasterization, at least 6.315 percent of time is saved in
vertices as those of fig. 4. It is hard to distinguish the most line rasterization(87.79 percent), almost the same
difference of the two figures by eyes. In order to see vision effect as that of Bresenham’s middle algorithm,
the actual difference, two enlarged figures by five and there is no line length limit in rasterization
times( each pixel is set in 5x5 pixels width), as showed process. Experiment shows that it is not easy to
in fig. 6 and fig. 7, respectively. See the three vertices distinguish the difference of the final images (rastered
of the triangles, a clear vision difference could be with Bresenham’s algorithm and our algorithm) by
noticed. eyes.
After enlarging both the two images by five times,
4. Conclusions clear difference could be noticed. So, when application
is viewed in original size, our algorithm could obtain
almost the same result with that of Bresenham’s

511
Figure 4. Triangle rastered by Bresenham’s
algorithm.
Figure 6. Bresenham’s algorithm rastered
triangle viewed in 5x5 pixels width for each
pixel.

Figure 5. Triangle rastered by our algorithm.

algorithm. Moreover, three multiplications are reduced


for each line’s rasterization process, and without line
length limit. This method could be used in simulation,
animation and visualization areas.
However, efficiency is obtained at the cost of Figure 7. Our algorithm rastered triangle viewed
precision with regard to our method. As mentioned in 5x5 pixels width for each pixel.
above, our algorithm’s maximum error is twice as that
of Bresenham’s. How to improve the rasterization [5] X. Wu and J.G. Ronkne, "Double-step Incremental
precision needs to do further research, especially when Generation of Lines and Circycles,” Computer Vision,
Graphics and Image Processing, vol. 37, no. 3, Mar. 1987,
use this method in numerical control and digital plotter
pp. 331-344.
areas. Moreover, How to check the real vision effect of [6] J.X. Chen, Guide to Graphics Software Tools, Springer,
simulation, animation and visualization need more and 2002, pp. 5-10.
more experiments and tests. Both we propose for [7] G.W. Gill, “N-step Incremental Straight-Line Algorithm,” IEEE
future work. Commuter Graphics and Applications, vol. 14, no. 3, May, 1994, pp.
66-72.
References [8] J.X. Chen et al., ”The Analysis and Statistic of Line
Distribution,” IEEE Computer Graphics and Applications,
vol. 22, no. 6, 2002, pp. 100-107.
[1] J. E. Bresenham, “Algorithm for Computer Control of Digital
[9] R.A. Earnshaw, “Line Tracking for Incremental Plotters,”
Plotter,” IBM Syst. J., vol. 4, no. 1, April 1965, pp. 25-30.
The Computer J., vol. 23, no. 1, Feb. 1980, pp. 46-52.
[2] J. D. Foley et al., Computer Graphics: Principles and
[10] C.M.A. Caste and M.L.V. Pitteway, “An Efficient
Practice, second Edition in C, Addition-Wesley, 1996, pp.
Structural Technique for Encoding Best-Fit Straight Lines,”
72-80.
The Computer J., vol. 30, no. 2, May 1987, pp. 168-175.
[3] J.G. Rokne et al., “Fast Line Scan-Conversion," ACM
[11] F.S. Hill, JR., Computer Graphics Using OpenGL,
Trans. on Graphics, vol. 9, no. 4, Oct. 1990, pp. 370-388.
second edition, Science, 2004, pp. 555-560.
[4] W.E. Wright, “Parallelization of Bresenham’s Line and
[12] D.F. Rogers, Procedural Elements for computer
Cycles Algorithms,“ IEEE Computer Graphics and
Graphics, China Machine and McGraw-Hill, 2002, pp.65-79.
Applications, vol. 10, no. 5, Sept. 1990, pp. 60-67.

512

S-ar putea să vă placă și