5 Line Drawing Algorithms.
5 Line Drawing Algorithms.
III
x1 x2
m<=1
1
Computer Graphics Downloaded from: http://www.bsccsit.com B.Sc. III
Here k takes value from starting point and increase by 1 until final end point. m can be
any real value between 0 and 1.
1 y
x k 1 x k m , y 1 y1
m x
x1 x2
m>1
The above equations are under the assumption that the lines are processed from left to
right. i.e. left end point is starting. If the processing is from right to left, we can
sample y 1 for line |m|<1
y k 1 y k m,
If |m|>1, y 1 and calculate
1
x k 1 xk .
m
2
Computer Graphics Downloaded from: http://www.bsccsit.com B.Sc. III
The DDA algorithm is faster method for calculating pixel position but it has problems:
m is stored in floating point number.
round of error
error accumulates when we proceed line.
so line will move away from actual line path for long line
xk xk+1
At sampling position xk+1, we label vertical pixel
separation from line path as d1& d2 as in figure .
The y-coordinate on the mathematical line path at pixel
column xk+1 is y=m(xk+1)+b
yk+1
d2
y d1
●
yk
xk+1
Then d1 = y - yk
= m(xk + 1)+b - yk
d2 =( yk + 1) - y
= (yk + 1)-m ( xk + 1 )-b
Now d1-d2 = 2m ( xk + 1 ) - ( yk + 1 )-yk+2b
= 2m (xk + 1) - 2yk + 2b – 1
A decision parameter pk for the kth step in the line algorithm can be obtained by
y
substituting m in above eqn and defining
x
3
Computer Graphics Downloaded from: http://www.bsccsit.com B.Sc. III
pk x(d1 d 2 )
y
x[2 ( xk 1) 2 y k 2b 1]
x
2y.xk 2x. y k 2y x(2b 1)
2y.xk 2x. y k c
Where the constant c 2y x(2b 1) which is independent.
If decision parameter pk is negative i.e. d1<d2, pixel at yk is closer to the line path than
pixel at yk+1 . In this case we plot lower pixel. ( xk+1 , yk). other wise plot upper pixel
(xk+1,yk+1).
Co-ordinate change along the line occur in unit steps in either x, or y direction.
Therefore we can obtain the values of successive decision parameters using incremental
integer calculations.
At step k+1, pk+1 is evaluated as.
pk 1 2y.xk 1 2xy k 1 c
pk 1 pk 2y( xk 1 xk ) 2x( y k 1 y k )
Since xk 1 xk 1
pk 1 pk 2y 2x( yk 1 y k )
The term y k 1 y k is either 0 or 1 depending upon the sign of p k .
The first decision parameter p0 is evaluated as.
po 2y x
and successively we can calculate decision parameter as
pk 1 pk 2y 2x( y k 1 y k )
so if pk is negative yk+1 = yk so pk+1 = pk+ 2 y
otherwise yk+1 = yk+1, then pk+1 = pk+ 2 y -2 x
Algorithm:
1. Input the two line endpoint and store the left endpoint at ( xo , yo )
2. Load ( xo , yo ) in to frame buffer, i.e. Plot the first point.
3. Calculate constants 2x,2y calculating x, y and obtain first decision
parameter value as
po 2y x
4. At each x k along the line, starting at k=0, perform the following test,
if pk 0, next point is ( xk 1, y k )
pk 1 pk 2y
otherwise
next point to plot is ( xk 1, y k 1)
pk 1 pk 2y 2x
4
Computer Graphics Downloaded from: http://www.bsccsit.com B.Sc. III
Function implementation in C
void lineBresenham (int x1, int y1, int x2, int y2)
{
int x, y, dx, dy, pk, k xEnd;
dx=abs(x2-x1);
dy=abs(y2-y1);
if(x1>x2)
{
x = x2;
y = y2;
xEnd = x1;
}
else
{
x = x1;
y = y1;
xEnd = x2;
}
putixel (x,y,1);
pk=2*dy-dx;
while (x<=xEnd)
{
if(pk<0)
{
x=x+1;
y=y;
pk=pk+2*dy;
}
else
{
x=x+1;
y=y+1;
pk= pk+2*dy-2*dx
}
putpixel (x,y,1);
}
}
For a line positive slope greater than 1, we simply interchange the role of x & y.