8

Click here to load reader

An algorithm for Line Drawing Using Parametric Equation

Embed Size (px)

Citation preview

Page 1: An algorithm for Line Drawing Using Parametric Equation

International Journal of Computer Graphics

Vol. 2, No. 1, May, 2011

9

An algorithm for Line Drawing Using Parametric Equation

Kumar S. Ray

Electronics & Communication Sciences Unit

Indian Statistical Institute 203, B.T. Road, Kolkata – 700108, India

Bimal Kumar Ray

School of Information Technology & Engineering

VIT University, Vellore – 632 014, India

Abstract

This paper presents two algorithms for drawing line using the parametric equations of

line segment. The first algorithm uses floating-point arithmetic and the second uses integer

arithmetic. The method of derivation of the algorithms presented here is borrowed from that

presented in Vector generation algorithm and the Bresenham’s algorithm. But both these

algorithms consider the slope-intercept form of line segment. Hence, in both the cases, the

line segments with sharp slope and those with gentle slope are to be considered separately.

But the proposed algorithms do not require the same. Thus the proposed algorithms reduce

the number of lines of code and computationally become more efficient.

Keywords Parametric equation, digitization, digital line

1. Introduction

Digital line generation is a fundamental tool of computer graphics. One needs an efficient

algorithm for generation of digital line segment especially for animated display. In absence of

efficient algorithm the animated display will deviate from realistic view. A number of

algorithms are already published in this area. The most famous of these is the Bresenham’s

algorithm that can be found in [1] and [2]. In [3] Danielsson presented an incremental method

for curve generation using parametric equations. It not only deals with lines but also with

other curves. In the same paper, Danielsson also talked about curve generation using non-

parametric equation. In [4] Pittway and Watkinson addressed the problem of line generation

with gray scale. This results in pleasing visual effect with display devices that permit multiple

levels of intensities. In [5] Dan Field presented two incremental linear interpolation

algorithms and analyzed their speed and accuracy. The first of these algorithms is a simple

digital differential algorithm employing fixed-point arithmetic and the second one is a new

algorithm which makes use of integer arithmetic and is a generalization of the Bresenham’s

line drawing algorithm. The algorithm is accurate and faster than the fixed-point algorithm

depending upon the underlying processor. The other algorithms for digital line generation can

be found in [6] through [10].

This paper presents two algorithms for digital line generation using the parametric

equations of line segment. The first algorithm uses floating-point arithmetic and the second

uses integer arithmetic only. The method of derivation of the algorithms presented here is

borrowed from that presented in Vector generation algorithm and the Bresenham’s algorithm

[2]. But both these algorithms consider the slope-intercept form of line segment. In both the

cases the line segments with sharp slope and those with gentle slope are to be considered

Page 2: An algorithm for Line Drawing Using Parametric Equation

International Journal of Computer Graphics

Vol. 2, No. 1, May, 2011

10

separately. But the proposed algorithms do not require the same. Hence the proposed

algorithm reduce the number of lines of code and computationally become more efficient. In

section 2 a trivial approach is presented which can be found in any textbook, in section 3 an

improvement over the trivial approach is shown. The algorithm presented in section 3

involves floating point arithmetic. In section 4 it is shown how the algorithm presented in

section 3 can be modified so as to produce an algorithm that uses integer arithmetic only.

2. A Trivial Approach

The parametric equations of a line segment with the end points (x1, y1) and (x2, y2) are

with 0 u 1. The length of the line segment is L = [(x2 – x1)2 + (y2 – y1)

2]

1/2 and the slope of

the line segment is m = (y2 – y1) / (x2 – x1). We propose to write dx = x2 – x1 and dy = y2 – y1.

One can derive a simple algorithm for line generation using the iterative formulae x = x +

dx*du and y = y + dy*du which can be obtained by taking differentials of the parametric

equations of the line segment. The step size of u (the value of du) can be computed using du =

1/L. Instead of using the conventional expression for the length of line segment, in this paper

we approximate L as L = |dx| + |dy|. This approximation of L will speed up the computation

and had also been found to reduce the jaggies (aliasing) from the line segment. This method

computes the full value of x and y and has been found to result in overdraw of pixels.

3. An Improvement

In order to avoid computation of the full value of x and y we introduce two variables,

namely d and h. The variable d will measure the horizontal distance of the mathematical line

segment from the nearest pixel center. The variable h will measure the vertical distance of the

mathematical line segment from the nearest pixel center. Without loss of generality it can be

assumed that the slope is positive and dx > 0. The first pixel along the digital line segment is

assumed to be (xa, ya). To determine the next pixel, we compute the vertical distance (h) of

the mathematical line segment using h = dx*du measured from the centerline of the pixel (xa,

ya). We also compute the horizontal distance (d) of the mathematical line segment from the

centerline of the pixel (xa, ya) using d. Compare each of h and d with 0.5. As long as d is less

than 0.5 keep incrementing d by the amount dx*du. When d is no longer less than 0.5

increment the x coordinate of the last pixel by 1. Also adjust the d value by adding dx*du to d

and subtracting 1 from the same. The decision parameter h and the ordinate y may be dealt

with in the same manner. The updated value of x and y are calculated in this way so that for a

new pixel position the h and d value are measured from the centerline of the new pixel. By

this process computation of the full value of h and d is avoided. The C source code for

generating the line segment is presented below. We have used a system dependent routine

namely, putpixel(x, y, intensity) to turn on the pixel (x, y) with the intensity value intensity.

The Figure 1 through Figure5 displays the output of this algorithm for different pair of end

points.

void VectorGeneration (float xa, float ya, float xb, float yb, int intensity)

{

float du, dx, dy, u, x, y, L, h, d, hx, hy, xstart, ystart, xend, yend;

int flag;

uyyyx

uxxxx

)(

)(

121

121

Page 3: An algorithm for Line Drawing Using Parametric Equation

International Journal of Computer Graphics

Vol. 2, No. 1, May, 2011

11

u = 0;

dx = xb – xa;

dy = yb – ya;

L = fabs(dx) +fabs(dy);

du = 1/L;

h = hx = fabs(dx) *du;

d = hy = fabs(dy) *du;

if ( dx > 0 ) {

xstart = xa;

xend = xb;

}

else {

xstart = xb;

xend = xa;

}

if ( dy > 0 ) {

ystart = ya;

yend = yb;

}

else {

ystart = yb;

yend = ya;

}

flag = (dx > 0);

if (dy < 0) flag = !flag;

if (flag) { // positive slope

x = xstart;

y = ystart;

while ( x <= xend && y <= yend ) {

putpixel (int(x + 0.5), int(y + 0.5), intensity);

if ( h < 0.5 ) h = h + hx;

if ( h >= 0.5) {

x = x + 1;

h = h + hx –1;

}

if ( d < 0.5 ) d = d + hy;

if ( d >= 0.5 ) {

y = y + 1;

d = d + hy –1;

}

}

else { // negative slope

x = xstart;

y = yend;

while( x <= xend && y >= ystart) {

putpixel(int(x + 0.5), int(y + 0.5), intensity);

if (h < 0.5) h = h + hx;

Page 4: An algorithm for Line Drawing Using Parametric Equation

International Journal of Computer Graphics

Vol. 2, No. 1, May, 2011

12

if ( h >= 0.5 ) {

x = x + 1;

h = h + hx -1;

}

if (d < 0.5)

d = d + hy;

if (d >= 0.5) {

y = y - 1;

d = d + hy - 1;

}

}// end of while

}// end of negative slope

return;

}//end of routine

4. Integer Arithmetic Algorithm

The last algorithm is now converted into the one that involves integer arithmetic only. As

earlier it is assumed that the slope is gentle and positive and the first pixel on the line segment

is (xa, ya) and the last pixel is (xb, yb). The test performed in the last algorithm was if h 0.5

and this implies that 2h – 1 0. But as du is a floating-point number computed by du = 1/L so

also h. So we multiply both sides of the test expression 2h – 1 0 by L = |dx| + |dy| and we

get 2hL – L 0. We introduce g = 2hL – L. So h = (g + L)/2L. If hn and ho respectively denote

the old and the new value of h then hn = ho + hx which gives (gn + L)/2L = (go + L)/2L + dxdu,

which in turn reduces to gn = go + 2dx because Ldu = 1. So the test h < 0.5 reduces to if g < 0

and if the condition is true then g = g + 2dx. Again if h >= 0.5 then the new value of h should

be computed by the formula hn = ho + hx 1 which gives (gn + L)/2L = (go + L)/2L + dxdu

1, which in turn reduces to gn = go + 2dx – 2L. So the test if h 0 reduces to if g 0 and if the

condition is satisfied then g should be updated by the formula g = g + 2(dx – L). Similar

arguments will lead to the corresponding test condition for d and the updating formulae,

namely, if g 0 then g = g + 2dy and if g 0 then g = g + 2(dy – L). If we take into

account lines of all kinds of slopes then in the above test we have to replace dx and dy by their

absolute values. The C source code for the algorithm is given below. In the source code, the

symbols g and g have been replaced by h and d respectively and the increments in h and d

have been denoted by h1, h2 and d1, and d2 so as to maintain similarity with the last source

code. The Figure 1 through Figure5 displays the output of this algorithm for different pair of

end points.

void LineGeneration ( int xa, int ya, int xb, int yb, intensity)

{

int x, y, L;

int xa, ya, xb, yb, dx, dy, h, h1, h2, d, d1, d2, xstart, ystart, xend, yend, flag;

dx = xb - xa;

dy = yb - ya;

L = abs(dx) + abs(dy);

h = 2*abs(dx) - L;

d = 2*abs(dy) - L;

h1 = 2*abs(dx); h2 = 2*(abs(dx) – L);

d1 = 2*abs(dy); d2 = 2*(abs(dy) – L);

Page 5: An algorithm for Line Drawing Using Parametric Equation

International Journal of Computer Graphics

Vol. 2, No. 1, May, 2011

13

if (dx > 0) {

xstart = xa;

xend = xb;

}

else {

xstart = xb;

xend = xa;

}

if (dy >0) {

ystart = ya;

yend = yb;

}

else {

ystart = yb;

yend = ya;

}

flag = (dx > 0);

if (dy < 0) flag = !flag;

if (flag) {

x = xstart;

y = ystart;

while( x <= xend && y <= yend) {

putpixel(x, y, intensity);

if (h < 0) h = h + h1;

if (h >= 0) {

x = x + 1;

h = h + h2;

}

if (d < 0) d = d + d1;

if (d >= 0) {

y = y + 1;

d = d + d2;

}

}//while

} //if (flag)

else { // negative slope

x = xstart;

y = yend;

while( x <= xend && y>=ystart) {

putpixel(x, y, intensity);

if (h < 0) h = h + h1;

if (h >= 0) {

x = x + 1;

h = h + h2;

}

if (d < 0) d = d + d1;

if (d >= 0) {

y = y − 1;

Page 6: An algorithm for Line Drawing Using Parametric Equation

International Journal of Computer Graphics

Vol. 2, No. 1, May, 2011

14

d = d + d2;

}

}//end of while

}// end of else

}//end of routine

5. Conclusion

An alternative method of drawing line segment has been proposed using the parametric

equations. Two algorithms, one floating-point and the other integer-arithmetic, have been

derived for drawing lines. In contrast to the Bresenham’s algorithm, the proposed method

does not need to consider the lines with gentle slope and those with sharp slope separately.

Thus, the present algorithm is shorter than the Bresenham’s algorithm. The present algorithm

requires five additional variables which is an extra cost required over the Bresenham’s

algorithm.

References [1] Bresenham J. E., “Algorithm for Computer Control of a Digital Plotter”, IBM System Journal, vol.4, no. 1,

pp. 106 –111, 1965.

[2] Steven Harrington, “Computer Graphics - A Programming Approach”, McGraw-Hill International Editions, 2nd Ed., 1987.

[3] P. E. Danielsson, “Incremental Curve Generation”, IEEE Transactions on Computers, vol. C-19, pp.763-793,

1970.

[4] Pittway M.L.V. and Watkinson D.J., “Bresenham’s algorithm with gray scale”, Communications of the ACM, vol.23, pp.625-626, 1980.

[5] Field D. “Incremental Linear Interpolation”, ACM Transactions on Graphics, vol.4, pp.1-11, 1985.

[6] Loceff M., A new approach to high-speed computer graphics: The line”, vol.13, pp.56-66, 1980.

[7] Sproull R.F., “Using program transformations to derive line-drawing algorithms”, ACM Transactions on

Graphics, vol.1, pp.259-273, 1982.

[8] Tran-Thong, “A symmetric linear algorithm for line segment generation”, Computer and Graphics, vol.6, pp15-17, 1982.

[9] Kaufman A., “Efficient algorithm for 3-D scan-conversion of parametric curves, surfaces and volumes”, in proceedings of SIGGRAPH ’82, Computer Graphics, vol. 16, pp. 223-232, 1982.

[10] Earnshaw R.A., “Line generation for incremental and raster devices”, Computer Graphics, vol. 11, pp. 199-205, 1977.

(a) (b) (c)

Figure 1. Line Segment with End Points (3,5) and (12, 22): (a) VectorGeneration; (b) LineGeneration; (c) Bresenham’s Algorithm

Page 7: An algorithm for Line Drawing Using Parametric Equation

International Journal of Computer Graphics

Vol. 2, No. 1, May, 2011

15

(a) (b) (c)

Figure 2. Line Segment with End Points (15, 5) and (18, 22): (a) VectorGeneration; (b) LineGeneration; (c) BresenHam’s Algorithm

(a) (b) (c)

Figure 3. Line Segment with End Points (15, 29) and (5, 19):

(a) VectorGeneration; (b) LineGeneration; (c) Bresenham’s Algorithm

Page 8: An algorithm for Line Drawing Using Parametric Equation

International Journal of Computer Graphics

Vol. 2, No. 1, May, 2011

16