dda line algorithm

Upload: koolnash8784

Post on 18-Oct-2015

11 views

Category:

Documents


0 download

TRANSCRIPT

  • DDA Line Algorithm Computer Graphics

    2010

    THIYAGARAAJ M Wiziontech Solutions All Rights are Reserved @ 2010 | www.thiyagaraaj.com

  • 2010 DDA LINE ALGORITHM

    T H I Y A G A R A A J M | W W W . T H I Y A G A R A A J . C O M

    Page 2

    Contents

    CONTENTS 2

    1. LINE EQUATION 3

    2. LINE DDA ALGORITHM 4

    Step: 1 4

    Step: 2 4

    Step: 3 4

    Step: 4 5

    3. ADVANTAGE & DISADVANTAGE: 6

    Advant age: 6

    Disadvantage: 6

    4. ALGORITHM: A NAVE LINE-DRAWING ALGORITHM 7

    5. C PROGRAMMING 8

    6. ITERATION EXAMPLE: 9

    Iteration: 1 9

    Iteration: 2 9

    7. REFERENCES 10

  • 2010 DDA LINE ALGORITHM

    T H I Y A G A R A A J M | W W W . T H I Y A G A R A A J . C O M

    Page 3

    1. Line Equation

    The Cartesian slop-intercept equation for a straight line is

    y=mx+b --(1) with m->slope b->y intercept

    The 2 end points of a line segment are specified at a position(x1,y1)

    Determine the values for the slope m and y intercept b with the fo llowing calcu lation.

    here, slope m: m = ( y2 - y1) / ( x2 - x1 ) m= Dy / Dx ------ ( 2 ) y intercept b b=y1-mx1 ------ ( 3 )

    Algorithms for displaying straight line based on this equation

    y interval Dy from the equation

    m= Dy / Dx Dy= m. Dx ------ ( 4 ) Similarly x interval Dx from the equation

    m = Dy / Dx Dx = Dy /m ------- ( 5 )

  • 2010 DDA LINE ALGORITHM

    T H I Y A G A R A A J M | W W W . T H I Y A G A R A A J . C O M

    Page 4

    2. Line DDA Algorithm

    The digital d ifferential analyzer (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 = ( y2-y1 ) / 1 m = ( yk+1 yk ) /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 compute 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 = ( y2 y1 ) / -1 yk+1 = yk - m -------- ( 8 ) Iintervals Dy=1 and compute each successive y values.

  • 2010 DDA LINE ALGORITHM

    T H I Y A G A R A A J M | W W W . T H I Y A G A R A A J . C O M

    Page 5

    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.

  • 2010 DDA LINE ALGORITHM

    T H I Y A G A R A A J M | W W W . T H I Y A G A R A A J . C O M

    Page 6

    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.

  • 2010 DDA LINE ALGORITHM

    T H I Y A G A R A A J M | W W W . T H I Y A G A R A A J . C O M

    Page 7

    4. Algorithm: A nave line-drawing algorithm

    dx = x2 - x1 dy = y2 - y1 for x from x1 to x2 { y = y1 + (dy) * (x - x1)/(dx) p ixel(x, y) }

  • 2010 DDA LINE ALGORITHM

    T H I Y A G A R A A J M | W W W . T H I Y A G A R A A J . C O M

    Page 8

    5. C Programming

    void linedda(int xa,int ya,int xb ,int yb) {

    int dx=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=dx/(float)steps;

    y increment=dy/(float)steps;

    putpixel(round(x),round(y),2)

    for(k=0;k

  • 2010 DDA LINE ALGORITHM

    T H I Y A G A R A A J M | W W W . T H I Y A G A R A A J . C O M

    Page 9

    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(2,2)

    Iteration: 2

    for(k=1;k(3,3)

    it will be incremented upto the final end point is reached.

  • 2010 DDA LINE ALGORITHM

    T H I Y A G A R A A J M | W W W . T H I Y A G A R A A J . C O M

    Page 10

    7. References

    1. little drops - Computer Graphics http://i.thiyagaraaj.com/tutorials/computer-graphics

    2. little drops - Computer Graphics Using C Programming http://i.thiyagaraaj.com/tutorials/computer-graphics-programs-using-c-programming

    3. little drops - DDA Line Algorithm http://i.thiyagaraaj.com/articles/articles/dda-line-algorithm

    4. More Video Tutorials http://w3.thiyagaraaj.com

    Contents1. Line Equation2. Line DDA AlgorithmStep: 1Step: 2Step: 3Step: 4

    3. Advantage & Disadvantage:Advantage:Disadvantage:

    4. Algorithm: A nave line-drawing algorithm5. C Programming6. Iteration Example:Iteration: 1Iteration: 2

    7. References