Fast Algorithm For Line Rasterization by Using Slope 1
Fast Algorithm For Line Rasterization by Using Slope 1
Abstract y
?
(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
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
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;};
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
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.
512