2010
DDA Line Algorithm
Computer Graphics
THIYAGARAAJ M Wiziontech Solutions
All Rights a re Reserved @ 2010 | www.thi ya ga raaj.com
2010
DDA LINE ALGORITHM
Contents
CONTENTS 1. LINE EQUATION 2. LINE DDA ALGORITHM
Step: 1 Step: 2 Step: 3 Step: 4
2 3 4
4 4 4 5
3. ADVANTAGE & DISADVANTAGE:
Advant age: Disadvantage:
6
6 6
4. ALGORITHM: A NAVE LINE-DRAWING ALGORITHM 5. C PROGRAMMING 6. ITERATION EXAMPLE:
Iteration: 1 Iteration: 2
7 8 9
9 9
7. REFERENCES
10
THIYAGARAAJ M |
WWW. T H I YA G A RA A J. C OM
Page 2
2010
DDA LINE ALGORITHM
1. Line Equation
The Cartesian slop-intercept equation for a straight line is
y=mx+b with m->slope b->y intercept
The 2 end points of a line segment are specified at a position(x1,y1)
--(1)
Determine the values for the slope m and y intercept b with the fo llo wing calcu lation. here, slope m: m = ( y2 - y 1) / ( x2 - x1 ) m= Dy / Dx y intercept b b=y1-mx1 -----( 2)
------
(3)
Algorithms for displaying straight line based on this equation y interval Dy from the equation
m= Dy / Dx Dy= m. Dx Similarly x interval Dx fro m the equation m = Dy / Dx Dx = Dy / m ------ ( 4 )
-------
( 5)
THIYAGARAAJ M |
WWW. T H I YA G A RA A J. C OM
Page 3
2010
DDA LINE ALGORITHM
2. Line DDA Algorithm
The digital d ifferential analy zer (DDA) is a scan conversion line algorithm based on calculation either Dy or Dx. The line at unit intervals is one coordinate and determine corresponding integer values nearest line for the other coordinate. Consider first a line with positive slope.
Step: 1
If the slope is less than or equal to 1 ,the unit x intervals Dx=1 and compute each successive y values. Dx=1 m = Dy / Dx m = ( y 2-y1 ) / 1 m = ( y k+1 y k ) /1 yk+1 = yk + m -------- ( 6 )
Subscript k takes integer values starting from 1, for the first point and increment by 1 until the final end point is reached. m->any real numbers between 0 and 1 Calculate y values must be rounded to the nearest integer
Step: 2
If the slope is greater than 1, the roles of x any y at the unit y intervals Dy=1 and co mpute each successive y values. Dy=1 m= Dy / Dx m= 1/ ( x2-x1 ) m = 1 / ( xk+1 xk ) xk+1 = xk + ( 1 / m ) ------- ( 7 )
Equation 6 and Equation 7 that the lines are to be processed from left end point to the right end point.
Step: 3
If the processing is reversed, the starting point at the right Dx=-1 m= Dy / Dx m = ( y 2 y1 ) / -1 yk+1 = yk - m Iintervals Dy =1 and compute each successive y values.
-------- ( 8 )
THIYAGARAAJ M |
WWW. T H I YA G A RA A J. C OM
Page 4
2010
DDA LINE ALGORITHM
Step: 4
Here, Dy=-1
m= Dy / Dx m = -1 / ( x2 x1 ) m = -1 / ( xk+1 xk ) xk+1 = xk + ( 1 / m ) -------- ( 9 )
Equation 6 and Equation 9 used to calculate pixel position along a line with ve slope.
THIYAGARAAJ M |
WWW. T H I YA G A RA A J. C OM
Page 5
2010
DDA LINE ALGORITHM
3. Advantage & Disadvantage:
Advantage:
Faster method for calculating pixel position then the equation of a pixel position. Y=mx+b
Disadvantage:
The accumulation of round of error is successive addition of the floating point increments is used to find the pixel position but it take lot of time to compute the pixel position.
THIYAGARAAJ M |
WWW. T H I YA G A RA A J. C OM
Page 6
2010
DDA LINE ALGORITHM
4. Algorithm: A nave line-drawing algorithm
dx = x2 - x1 dy = y2 - y1 for x fro m x1 to x2 { y = y 1 + (dy) * (x - x1)/(d x) p ixel(x, y) }
THIYAGARAAJ M |
WWW. T H I YA G A RA A J. C OM
Page 7
2010
DDA LINE ALGORITHM
5. C Programming
void linedda(int xa,int ya,int xb ,int yb) int d x=xb-xa,dy=yb-ya,steps,k; float xincrement,yincrement,x=xa,y=ya; if(abs(dx)>abs(dy)) steps=abs(dx); else steps=abs(dy); xincrement=d x/(float)steps; y increment=dy/(float)steps; putpixel(round(x),round(y),2) for(k=0; k<steps;k++) { x+=xincrement; y+=y increment; putpixel(round(x),round(y),2); } } {
THIYAGARAAJ M |
WWW. T H I YA G A RA A J. C OM
Page 8
2010
DDA LINE ALGORITHM
6. Iteration Example:
If take, xa,ya=>(2,2) xb ,yb=>(8,10) dx=6 dy=8 xincrement=6/ 8=0.75 yincrement=8/ 8=1
Iteration: 1
for(k=0;k<8; k++) xincrement=0.75+0.75=1.50 yincrement=1+1=2 1=>(2,2)
Iteration: 2
for(k=1;k<8; k++) xincrement=1.50+0.75=2.25 yincrement=2+1=3 2=>(3,3) it will be incremented upto the final end point is reached.
THIYAGARAAJ M |
WWW. T H I YA G A RA A J. C OM
Page 9
2010
DDA LINE ALGORITHM
7. References
1. 2. 3. little drops - Computer Graphics http://i.thiyagaraaj.com/tutorials/computer-graphics little drops - Computer Graphics Using C Programming http://i.thiyagaraaj.com/tutorials/computer-graphics-programs-using-c-programming little drops - DDA Line Algorithm http://i.thiyagaraaj.com/articles/articles/dda-line-algorith m
4. More Video Tutorials http://w3.thiyagaraaj.com
THIYAGARAAJ M |
WWW. T H I YA G A RA A J. C OM
Page 10