Search code examples
algorithmgraphicsdda

Confused in DDA algorithm , need some help


I need help regarding DDA algorithm , i'm confused by the tutorial which i found online on DDA Algo , here is the link to that tutorial

http://i.thiyagaraaj.com/tutorials/computer-graphics/basic-drawing-techniques/1-dda-line-algorithm

Example:

xa,ya=>(2,2)
xb,yb=>(8,10)
dx=6
dy=8

xincrement=6/8=0.75
yincrement=8/8=1

1)     for(k=0;k<8;k++)
xincrement=0.75+0.75=1.50
yincrement=1+1=2
1=>(2,2)

2)     for(k=1;k<8;k++)
xincrement=1.50+0.75=2.25
yincrement=2+1=3
2=>(3,3)

Now i want to ask that , how this line came xincrement=0.75+0.75=1.50 , when it is written in theory that

"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 )

"

it should be xincrement=x1 (which is 2) + 0.75 = 2.75

or i am understanding it wrong , can any one please teach me the how it's done ?

Thanks a lot)


Solution

  • There seems to be a bit of confusion here.

    To start with, let's assume 0 <= slope <= 1. In this case, you advance one pixel at a time in the X direction. At each X step, you have a current Y value. You then figure out whether the "ideal" Y value is closer to your current Y value, or to the next larger Y value. If it's closer to the larger Y value, you increment your current Y value. Phrased slightly differently, you figure out whether the error in using the current Y value is greater than half a pixel, and if it is you increment your Y value.

    If slope > 1, then (as mentioned in your question) you swap the roles of X and Y. That is, you advance one pixel at a time in the Y direction, and at each step determine whether you should increment your current X value.

    Negative slopes work pretty much the same, except you decrement instead of incrementing.