Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
37 views

5 Line Drawing Algorithms.

The document summarizes algorithms for drawing lines in computer graphics, including: - The slope-intercept equation for a line and calculating slope from two points. - Incremental and DDA (Digital Differential Analyzer) line drawing algorithms, which have issues with floating point errors accumulating over long lines. - Bresenham's line algorithm, which uses only integer calculations to determine pixel positions, avoiding incremental errors. It works by calculating a decision parameter to choose whether to plot above or below the line at each step.

Uploaded by

rupak dangi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

5 Line Drawing Algorithms.

The document summarizes algorithms for drawing lines in computer graphics, including: - The slope-intercept equation for a line and calculating slope from two points. - Incremental and DDA (Digital Differential Analyzer) line drawing algorithms, which have issues with floating point errors accumulating over long lines. - Bresenham's line algorithm, which uses only integer calculations to determine pixel positions, avoiding incremental errors. It works by calculating a decision parameter to choose whether to plot above or below the line at each step.

Uploaded by

rupak dangi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Computer Graphics Downloaded from: http://www.bsccsit.com B.Sc.

III

Line Drawing Algorithms.


The slope-intercept equation of a straight line is:
y  mx  b where m = slope of line and , b = y-intercept.
for any two given points ( x1 , y1 ) and ( x2 , y 2 )
y  y1
slope (m) = 2
x 2  x1
y  y1
b  y  2 x from above equation i.e. y  mx  b
x 2  x1
At any point ( xk , y k )
y k  mxk  b ……………. 1
At ( xk 1 , y k 1 ) ,
y k 1  mxk 1  b ……………2.

subtracting 1 from 2 we get-


y k 1  y k  m( xk 1  xk )
Here ( y k 1  y k ) is increment in y as corresponding increment in x.
 y  m.x
y
or m 
x
For incremental algorithm in line drawing ,
 Increment x by 1
 Computer corresponding y and display pixel at position (xi, round (yi) )

Problem: Floating point multiplication & addition


 The round function.

DDA line Algorithm:


The digital differential analyzer (DDA) is a scan conversion line drawing algorithm based
on calculating either x or y form the equation,
y  m.x.
We sample the line at unit intervals in one co-ordinate and determine the corresponding
integer values nearest to the line path for the other co-ordinates.
Consider a line with positive slope.
If m<=1, we sample x co-ordinate. So
x  1 and compute each successive y y2
value as:
y
y k 1  y k  m m  , x  1
x y1

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.

For line with positive slope greater than 1,


we sample y  1 and calculate y2
corresponding x values as

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

The complete C function for DDA algorithm is.


void lineDDA (in x1, int y1, int x2, int y2)
{
int dx, dy, steps, k;
float incrx; incry; x,y;
dx=x2-x1;
dy=y2-y1;
if (abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
incrx=dx/steps;
incry=dy/steps;
x=x1; /* first point to plot */
y=y1;
putpixel(round(x), round(y),1); //1 is used for color
for (k=1;k<=steps;k++)
{
x = x + incrx;
y = y + incry;
putpixel(round(x),round(y),1);
}
}

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

Bresenham's Line algorithm:


An accurate and efficient line generating algorithm, developed by Bresenham that
scan converts lines only using integer calculation to find the next (x,y) position to plot. It
avoids incremental error accumulation.

Line with positive slope less than 1 (0<m<1)


Pixel position along the line path are determined
by sampling at unit x intervals. Starting from left
end point, we step to each successive column and
plot the pixel closest to line path.
Assume that (xk,yk) is pixel at kth step then next yk+1
point to plot may be either ( xk  1, y k ) or yk
( xk  1, y k  1)

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
 2y.xk  2x. y k  2y  x(2b  1)
 2y.xk  2x. y k  c
Where the constant c  2y  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  2y.xk 1  2xy k 1  c
 pk 1  pk  2y( xk 1  xk )  2x( y k 1  y k )
Since xk 1  xk  1
 pk 1  pk  2y  2x( 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  2y  x
and successively we can calculate decision parameter as
pk 1  pk  2y  2x( 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 2x,2y calculating x, y and obtain first decision
parameter value as
po  2y  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  2y
otherwise
next point to plot is ( xk  1, y k  1)
pk 1  pk  2y  2x

5. Repeat step 4 x times.

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);
}
}

Brasenham's algorithm is generalized to lines with arbitrary slope by considering the


symmetry between the various octants & quadrants of xy plane.

For a line positive slope greater than 1, we simply interchange the role of x & y.

You might also like