COMPUTER GRAPHICS


                                                          (COMPUTER GRAPHICS)

                          DDA   ALGORITHM     
                                                                                          
                    
       BASIC

       -  The full form of the DDA algorithm is the Digital Differential analyzer algorithm.
            
            we know that equation of the line is y = mx + c,  where m is the slope of the line.
          
          different type of equation of line     1.  y-y1 = m (x-x1)/(x2-x1)  , where m = y2-y1/x2-x1
                                                                    2. x/a + y/b =1  where  y intercept is b 
                                                                                                          x intercept is a


         - In DDA algorithm we have to print the line in the graphic console.
              


          Explanation : 

                 let two coordinate of given line is (x1,y1)  and (x2,y2)
                   1.   calculate dx  = x2-x1  and dy = y2-y1
                   2.   calculate the maximum between mod ( |-2| = 2 or |3| = 3 ) of dx and dy. and store max value in steps.
                    
                  3.  take a variable Xincr = dx/steps  and Yincr= dy/steps.
                  4  last increase the  x1 and y1 on adding Xincr and Yincr and put pixel in any color.
                          x1 = x1 +Xincr
                           y1  = y1 + Yincr
              note  Xk+1= Xk + 1;   Yk+1 = Yk +1






       Code  in C++

                
             #include<iostream>
             #include<graphics.h>
            #include<conio.h>
             using namespace std;



void DDA(int x1,int y1,int x2, int y2)
{
int dx=x2-x1,dy=y2-y1;
int step;
if(abs(dx)>abs(dy))
{
step=abs(dx);
}

else
{
   step=abs(dy);
}

float m=dy/dx;
float Xincr=dx/step;
float Yincr=dy/step;
for(int i =0;i<=step;i++)
{
putpixel(x1,y1,WHITE);
x1=x1+Xincr;
y1=y1+Yincr;
                   delay(100);
}
}

int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
 int x1,y1,x2,y2;
 cin>>x1>>y1>>x2>>y2;
 
    DDA(x1, x2, x2, y2); 
getch();
return 0;
}   



The drawback of the DDA algorithm
1. It takes a long time.
2. It has less accuracy.







Thanks



   
           

                                                  

Comments

Popular Posts