Transcript
Page 1: Fast algorithm for line rasterization by using slope 1

Fast Algorithm for Line Rasterization by Using Slope 1

Hua Zhang*,† Changqian Zhu* Jun Yang*

[email protected] [email protected] [email protected] *Dept. of Computer and Communication Engineering, Southwest Jiaotong University

†Institute of Computer Applications, China Academy of Engineering Physics

Abstract

In this paper, a fast algorithm for line rasterization

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 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. 1. Introduction

Line rasterization is a basic subject of digital plotter, numerical control, and computer graphics. There are many papers, which discussed the efficiency and implementation of line rasterization, such as [1], [3], [4], [5], [7], [8], [9], [10] and [11]. Among these papers, Bresenham’s middle point algorithm [1] is the most classical and widely used algorithm, which has the advantages of raster precision and calculation efficiency, and only integer operation is needed.

Although Bresenham’s middle algorithm is fast enough, numerous applications require higher speed, such as real simulation, visualization of large data sets, and rendering more complex objects. Many algorithms have discussed this problem. There are two kinds of algorithms for fasting the Bresenham’s middle algorithm. One use hardware, such as [5], [7], [9], and [10]. Wu et al. [5] present a fast algorithm for line rasterization based on double-step technology. Gill [7] presents his fast algorithm which takes advantage of N-step technology. Earnshaw [9] and Castle et al. [10] take advantage of the repeated patterns that algorithm generates strait lines. On the other hand, Rokne [3] and Wright [4] present parallelization method for fasting the Bresenham’s middle algorithm.

y

x

?

(0, 0)

(1, 1)

?(1, 0)

(0, 1)

? (1, 1/2)

Figure 1. Basis of Bresenham’s algorithm.

Since the upper fast algorithms just emphasize on algorithm itself, How about line in practical usage is? In a recent paper, Chen, Wang and Bresenham [8] give out detailed and clear results about this question after studying many current line drawing packages. One of the most important results is that 87.79 percent lines are less than or equal to 17 pixels. So, in this case, the fast algorithm based on the longer length of strait line may not have more advantage than we expect before. In this paper, a faster algorithm based on Bresenham’s middle algorithm without line length limits is presented.

Now, let us to consider this question: why should we raster the line? One of the important reasons is to show the natural objects on screen. Surely, when considering in digital plotter and numerical control, the precision is the most important factor. This paper just emphasize on the drawing lines on screen display. If a final result on scene can not be distinguished the difference between two images (rendered with different method) by eyes, approximate method could be a selection if it has some advantages, such as calculation efficiency(Bresenham’s middle algorithm

5080-7803-8963-8/05/$20.00 ©2005 IEEE

Page 2: Fast algorithm for line rasterization by using slope 1

Table 1. Breseham’s algorithm

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

int dx,dy,inCreE,inCreNE,d,x,y,flag=0;

if (xn<x0) { swapd(&x0,&xn);swapd(&y0,&yn);};if (yn<y0) { y0=-y0,yn=-yn,flag=10;};dy=yn-y0; dx=xn-x0;if (dx<dy){

swapd(&x0,&y0);swapd(&xn,&yn);swapd(&dy,&dx);flag++;

}

x=x0;y=y0;d=2*dy-dx;inCreE=2*dy;inCreNE=2*(dy-dx);

while(x<xn+1){writepixel(x,y,flag);x++;if (d<=0) d+=inCreE;else {y++,d+=inCreNE;}

}}

Where d is error for representing the line, inCreE is the error in east direction, inCreNE is the error in northeast direction, and swapd is a function of exchanging the values of two variables.

Table 2. Our algorithm.

//Assume two end coordinates are inter, as supposed by Bresenham.void ourline(int x0, int y0, int xn, int yn){

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

dy=yn-y0; dx=xn-x0;if (dx<dy){

swapd(&x0,&y0);swapd(&xn,&yn);swapd(&dy,&dx);flag++;

}x=x0;y=y0;d=dy-dx;inCreE=dy;inCreNE=dy-dx;while(x<xn+1){

writepixel(x,y,flag);x++;if (d<0) d+=inCreE;else {y++,d+=inCreNE;}

}}

Where d is error for representing the line, inCreE is the error in east direction, inCreNE is the error in northeast direction, and swapd is a function of exchanging the values of two variables.

Start

xn < x0 swapd(&x0,&xn)swapd(&y0,&yn)

yn < y0y0=-y0yn=-ynflag=10

dy = yn - y0dx = xn - x0

dx < dy

swapd(&x0,&xn)swapd(&y0,&yn)swapd(&dx,&dy)

flag ++

x = x0y = y0

d = dy - dxinCreE = dy

inCreNE = dy - dx

x < xn + 1

writepixel(x,y,flag)x ++

d < 0

y ++d += inCreNE

d += inCreE

End

yes

yes

yes

yes

yes

no

no

no

no

no

Figure 2. Flowchart of our algorithm.

is also an approximate algorithm for 2D raster display, although it has the smallest error to represent line).

In this paper, we always assume the slope of line has the range of 0 to 1. Other lines could be obtained by reflecting along principal axes. In fig. 1, for Bresenham’s algorithm, if the slope of line is less than 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 the sign of error is positive, the northeast pixel is selected. If the sign of error is negative or equal to zero, the east pixel is selected. The C program [6] for

509

Page 3: Fast algorithm for line rasterization by using slope 1

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 algorithm. Let d be the error term, inCreE be error term when the east pixel is selected, and inCreNE be the error term when the northeast error term is selected. Now the error term d at the first pixel from end point could be represented as following:

d = -1 +deltay/deltax (1)

Since deltax is larger than zero, multiplying both

sides of (1) by deltax, we could obtain:

d x deltax = deltay - deltax

Because d has same sign as that of d x deltax and only the sign of error term is needed to raster the line, let d = d x deltax. If the east point is selected, another error deltay/deltax is introduced, named as inCreE. If the northeast pixel is selected, error deltay/deltax - 1 is introduced, named as inCreNE. So, variables inCreE and inCreNE could be obtained as following:

inCreE = deltay

inCreNE = deltay - deltax The C code of this algorithm could be represented

in Table 2. Note that the bold code is different from those of table 1. Compared with Bresenham’s algorithm (see table 1.), three multiplications are reduced for each line rasterization process; and the logical comparison calculation is changed from <= to <. Figure 2 is a flowchart of our algorithm. Since there are only three multiplications are saved in initializing step, not in inner loop, when rastering a line compared with Bresenham’s algorithm, are there any merits for our algorithm?

Figure 3. The ratio between our time with Bresenham’s time in percent.

Where horizontal axis is the y coordinates of the second end points, and the vertical axis is the time ratio. 3. Comparisons 3.1. Efficiency

When start to raster a line, we also assume the coordinates of two end points are integers, as supposed and used in many line rasterization algorithms, including in software and hardware, such as in [4], [5], [7], and [10]. If the coordinates are not integers, round computation could be used [1].

Compared with table 1, table 2 (see bold code) decreased three multiplication computations in each line rasterization process, and need not to consider the line length limit. That is to say, in whatever the length of line, three multiplications are saved for each line rasterization process.

Since Chen et al. [8] present a statistical result that 87.79 percent of lines are shorter than or equal to 17 pixels, we take advantage of this result without losing generality, and select 15 pixel length in x directional scan line. With regard to slope, we test slope from 0 to 1 in first octant( using this method, a result about how the slope affect the efficiency could be gotten). Line in the other octants could be obtained by being reflected along with principal axes.

Our testing environment is a personal computer, which includes Intel PIII 500 CPU, RAM 384M, and Operating System is Windows 2000 professional.

From table 3, we can note that the maximum ratio

510

Page 4: Fast algorithm for line rasterization by using slope 1

is 93.685 percent. That is to say, 6.315 percent of time 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) to point (15, 9)). An explicit curve about changing trend is showed in fig. 3. The zigzag figure is affected 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 algorithm from slope 0 to 1 in many(exactly 87.79 percent) actual rasterizations. 3.2. Precision

Let us see fig. 1, middle scan line is the decision variable for Bresenham’s middle algorithm, and the max error in vertical direction is 0.5 dot distance(Note that the line can be in any directions. That is to say, the error measured by vertical scan line, not by the right line from pixel centre to the line in theory). However, in our algorithm, the max error in vertical directions is one dot distance. So, our algorithm has two times error as that of Bresenham’s middle algorithm. Surely, in precision case, our algorithm is not better than that of Bresenham’s, and even worse compared with Bresenham’s middle algorithm. Our algorithm can not be used in the areas requiring high interpolation precision, such as digital plotter and numerical control. How about computer displaying is when using our algorithm? Is this the same result as that of areas requiring high precision? The following section may give some hints for the answers. 3.3. Visual effect

Since triangle is the current popular element used to approximate to facet objects, two triangles are used in this paper to show the difference of vision by using our algorithm and Bresenham’s middle algorithm. Fig. 4 is a triangle rendered by using Bresenham’s middle point algorithm. Fig. 5, which is rendered by our algorithm, is a triangle with same coordinates of vertices as those of fig. 4. It is hard to distinguish the difference of the two figures by eyes. In order to see the actual difference, two enlarged figures by five times( each pixel is set in 5x5 pixels width), as showed in fig. 6 and fig. 7, respectively. See the three vertices of the triangles, a clear vision difference could be noticed. 4. Conclusions

Table 3. Efficiency Comparison

(x0,y0,xn,yn)Bresenham's

Algorithm(µs)

Our Algorithm(µs)

Our time / Bresehamtime%

(0,0,15,0) 476.517 446.425 93.685(0,0,15,1) 502.578 468.495 93.218

(0,0,15,2) 528.559 490.565 92.811(0,0,15,3) 506.489 472.406 93.270

(0,0,15,4) 484.419 446.705 92.214

(0,0,15,5) 510.400 472.686 92.610(0,0,15,6) 560.127 520.457 92.917

(0,0,15,7) 540.571 494.476 91.472(0,0,15,8) 492.521 446.705 90.697(0,0,15,9) 582.197 514.311 88.339

(0,0,15,10) 552.584 492.531 89.132(0,0,15,11) 594.489 544.203 91.541

(0,0,15,12) 556.495 496.711 89.257(0,0,15,13) 574.375 522.413 90.953

(0,0,15,14) 528.559 472.965 89.481(0,0,15,15) 530.514 470.425 88.673

Where x0, y0, xn, and yn are end coordinates of two end points. This table is based on the following testing environment: Intel PIII 500M, RAM 384M, and Windows 2000 Professional operating system.

A method of line rasterization is presented in this paper. Compared with Bresenham’s middle point algorithm, our algorithm has following advantages: three multiplications are reduced for each line rasterization, at least 6.315 percent of time is saved in most line rasterization(87.79 percent), almost the same vision effect as that of Bresenham’s middle algorithm, and there is no line length limit in rasterization process. Experiment shows that it is not easy to distinguish the difference of the final images (rastered with Bresenham’s algorithm and our algorithm) by eyes.

After enlarging both the two images by five times, 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

Page 5: Fast algorithm for line rasterization by using slope 1

Figure 4. Triangle rastered by Bresenham’s algorithm.

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 precision with regard to our method. As mentioned above, our algorithm’s maximum error is twice as that of Bresenham’s. How to improve the rasterization precision needs to do further research, especially when use this method in numerical control and digital plotter areas. Moreover, How to check the real vision effect of simulation, animation and visualization need more and more experiments and tests. Both we propose for future work. References [1] J. E. Bresenham, “Algorithm for Computer Control of Digital Plotter,” IBM Syst. J., vol. 4, no. 1, April 1965, pp. 25-30. [2] J. D. Foley et al., Computer Graphics: Principles and Practice, second Edition in C, Addition-Wesley, 1996, pp. 72-80. [3] J.G. Rokne et al., “Fast Line Scan-Conversion," ACM Trans. on Graphics, vol. 9, no. 4, Oct. 1990, pp. 370-388. [4] W.E. Wright, “Parallelization of Bresenham’s Line and Cycles Algorithms,“ IEEE Computer Graphics and Applications, vol. 10, no. 5, Sept. 1990, pp. 60-67.

Figure 6. Bresenham’s algorithm rastered triangle viewed in 5x5 pixels width for each

pixel.

Figure 7. Our algorithm rastered triangle viewed in 5x5 pixels width for each pixel.

[5] X. Wu and J.G. Ronkne, "Double-step Incremental Generation of Lines and Circycles,” Computer Vision, Graphics and Image Processing, vol. 37, no. 3, Mar. 1987, pp. 331-344. [6] J.X. Chen, Guide to Graphics Software Tools, Springer, 2002, pp. 5-10. [7] G.W. Gill, “N-step Incremental Straight-Line Algorithm,” IEEE Commuter Graphics and Applications, vol. 14, no. 3, May, 1994, pp. 66-72. [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. [9] R.A. Earnshaw, “Line Tracking for Incremental Plotters,” The Computer J., vol. 23, no. 1, Feb. 1980, pp. 46-52. [10] C.M.A. Caste and M.L.V. Pitteway, “An Efficient Structural Technique for Encoding Best-Fit Straight Lines,” The Computer J., vol. 30, no. 2, May 1987, pp. 168-175. [11] F.S. Hill, JR., Computer Graphics Using OpenGL, second edition, Science, 2004, pp. 555-560. [12] D.F. Rogers, Procedural Elements for computer Graphics, China Machine and McGraw-Hill, 2002, pp.65-79.

512


Recommended