An Algorithm For Line Drawing Using Parametric Equation PDF
An Algorithm For Line Drawing Using Parametric Equation PDF
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 Bresenhams
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 nonparametric 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 Bresenhams
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 Bresenhams 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
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
x x1 ( x2 x1 )u
x y1 ( y2 y1 )u
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;
10
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;
11
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
12
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;
13
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 Bresenhams 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 Bresenhams algorithm. The present algorithm
requires five additional variables which is an extra cost required over the Bresenhams
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., Bresenhams 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. 199205, 1977.
(a)
(b)
(c)
Figure 1. Line Segment with End Points (3,5) and (12, 22):
(a) VectorGeneration; (b) LineGeneration; (c) Bresenhams Algorithm
14
(a)
(b)
(c)
Figure 2. Line Segment with End Points (15, 5) and (18, 22):
(a) VectorGeneration; (b) LineGeneration; (c) BresenHams Algorithm
(a)
(b)
(c)
Figure 3. Line Segment with End Points (15, 29) and (5, 19):
(a) VectorGeneration; (b) LineGeneration; (c) Bresenhams Algorithm
15
16