Thursday, January 14, 2010

DDA Line drawing algorithm

Hi,

Happy makar sankrant to all.


Advantages & disadvantages of DDA:-
• Faster than the direct use of the line equation and it does not do any floating point multiplication.
• Floating point Addition is still needed.
• Precision loss because of rounding off.
• Pixels drift farther apart if line is relatively larger.
• The basis of the DDA (Digital Differential Analyzer) method is to take unit steps along one of the coordinate assume x co-ordinate and compute the corresponding values along the other coordinate lets say Y co-ordinate. The unit steps Lets say m are always along the coordinate of greatest change, e.g. if we have dx = 11 and dy = 6, then we would take unit steps along x and compute the steps m along y.
The slope m is calculated as:-

slope = (m = dy/dx)
if (m <= 1.0) then //Means we have dx > dy

let x_step = 1 {dx = 1, dy = 0 or 1}
else {m > 1.0} //We have dx < dy
let y_step = 1 {dy = 1, x_step = 0 or 1 }



DDA Line drawing function in C:

void DDALine( int x1, int y1,int x2,int y2);
{
int dx, dy, steps;
float x_inc, y_inc, x, y;

//
dx = x2 - x1;
dy = y2 - y1;

if (abs(dx) > abs(dy))
steps = abs(dx); //steps is larger of dx, dy
else
steps = abs(dy);

x_inc = dx/steps;
y_inc = dy/steps;

//either x_inc or y_inc = 1.0, the other is the slope

x=x1;
y=y1;
putxy(round(x), round(y));

for( i = 1 to steps) {
x = x + x_inc;
y = y + y_inc;
putxy(round(x), round(y));
}
}//End DDALine Algorithm Function


Now that we have seen DDA line drawing algorithm. I want you to draw a line in all quadrants using the same logic



Consider slope m as
1. m= 1
2. m < 1
3. m > 1
4 Positive and negative slope

Write your observations in the conclusion.

Next week (18-22) show me 2 assignments:
1. DDA line drawing
2. Any scenary created using graphics library function. (Try to animate if possible). Make use of different colors and shapes.

Have a nice day

No comments:

Post a Comment