DDA Line Drawing Algorithm

 

Note:Turn on Desktop View for better visual :)


o   DDA Line Drawing Algorithm


            DDA stands for Digital Differential Analyzer.

            The Digital Differential Analyzer helps us to interpolate the variables on an interval from one                 point to another point. We can use the digital Differential Analyzer algorithm to perform                         rasterization on polygons, lines, and triangles.

            Digital Differential Analyzer algorithm is also known as an incremental method of scan                           conversion. In this algorithm, we can perform the calculation in a step by step manner. We use                the previous step result in the next step.



o   Derivation


            Consider the equation of a line  :  y= mx +b.

            where m is the slope of line and b is the intercept of line.

            We know that,               

            Dx = (x2 – x1)  and  Dy = (y2 – y1)

            m = dy / dx

            Here (x1, y1) and (x2, y2) are the endpoints of a line. Further, We can derive the value of dy

            dy=m dx ——————- eq1

            Similarly, we can derive the value of dx

            dx=(dy)/m ——————-eq2

            We sample the line by incrementing or decrementing by 1 in one coordinate. Then determine                 corresponding nearest integer values along the line path for the other coordinate.


            Calculating Successive values of y

            We increment Δx by one unit and we will calculate successive values of y with the help of the                following equation:

            yi+1 = yi + dy

            where yi is the initial value of y in any step of calculating points for the straight line.

            Further, Substitute the value of dy from eq1

            yi+1= yi + m dx                  —————— (eq3)


            This is the main equation used to calculate successive values of y along the line for rasterizing                or scan converting the line. It means if we increment dx by one unit then successive values of y             will be calculated according to (eq3)

            Put  dx = 1 in eq3 and you get

            yi+1= yi + m


            Calculating Successive values of x

            On the other hand, if we increment dy by one unit then we will calculate successive values of x             with the help of the following equation.

            xi+1= xi+ dx

            Where xi is the initial value of x in any step of calculating points for the straight line for                        instance.

            Further, Substitute the value of dx from eq2

            xi+1= xi+ dy/m                       —————————— (eq4)

            Put dy = 1 in eq4 and you get.

            xi+1= xi +1/ m



o   Algorithm  


Step1: Start

Step2: Declare x1,y1,x2,y2,dx,dy,x,y variables.

Step3: Enter value of x1,y1,x2,y2.

Step4: Calculate dx = x2-x1

Step5: Calculate dy = y2-y1

Step6: If abs(dx) > abs(dy)

            Then step = abs(dx)

            Else

                  Step = abs(dy)

Step7: xinc=dx/step

            yinc=dy/step

            assign x = x1

            assign y = y1

Step8: Set pixel (x, y)

Step9: x = x + xinc

            y = y + yinc

            Set pixels (Round (x), Round (y))

Step10: Repeat step 9 until step

Step11: End




o   Slope cases  

 

Case1: When |m|<1

1)     (dx > 0 & dy > 0)

Xi+1 = Xi + 1

Yi+1 = Yi + m

 

2)     (dx > 0 & dy < 0)

Xi+1 = Xi + 1

Yi+1 = Yi - m

 

3)     (dx < 0 & dy > 0)

Xi+1 = Xi - 1

Yi+1 = Yi + m

 

4)     (dx < 0 & dy < 0)

Xi+1 = Xi - 1

Yi+1 = Yi - m

 

 

Case2: When |m|<1

1)     (dx > 0 & dy > 0)

Xi+1 = Xi + (1/m)

Yi+1 = Yi + 1

 

2)     (dx > 0 & dy < 0)

Xi+1 = Xi + (1/m)

Yi+1 = Yi - 1

 

3)     (dx < 0 & dy > 0)

Xi+1 = Xi – (1/m)

Yi+1 = Yi + 1

 

4)     (dx < 0 & dy < 0)

Xi+1 = Xi – (1/m)

Yi+1 = Yi – 1

 


Case3: When |m| = 1

                  Xi+1 = Xi + 1

                  Yi+1 = Yi + 1

                 

 




 

·       Problem Statements :

 

1)    To draw a line with line style Thin, Thick, Dotted, Dash by using DDA.

à

·     Source Code

 

#include<iostream>

using namespace std;

#include<conio.h>

#include<graphics.h>

#include<math.h>

 

class DDA

{

            int dx, dy;                   

            float m;

            float Xi, Yi, Xinc, Yinc;

            int steps;

           

            public:

           

            // To draw a thin line

            void DDA_ThinLine(int x1, int y1, int x2, int y2){

           

                        dx = x2 - x1;

                        dy = y2 - y1;

           

                        if(abs(dx) > abs(dy)){

                                    steps = abs(dx);

                        }

                        else{

                                    steps = abs(dy);

                        }

           

                        // Incremental values

                        Xinc = float(dx) / steps;

                        Yinc = float(dy) / steps;

           

                        Xi = x1;

                        Yi = y1;

                        putpixel(Xi, Yi, WHITE);          // starting points

           

                        for(int i = 1; i <= steps; i++)

                        {

                                    Xi = Xi + Xinc;

                                    Yi = Yi + Yinc;

                                    delay(5);

                                    putpixel(round(Xi), round(Yi), WHITE);

                                    cout<<"Points plotted : ("<<Xi<<","<<Yi<<") Steps : "<<i<<endl;

                        }

           

            }

           

            // To draw thik line

            void DDA_ThikLine(int x1, int y1, int x2, int y2, int w){

                       

                        float wx, wy;

                       

                        DDA_ThinLine(x1, y1, x2, y2);

                        if ((y2 - y1) / (x2 - x1) < 1)

                        {

wy = (w-1)*sqrt(pow((x2-x1),2)+pow((y2-y1),2))/(2*fabs(x2-x1)); 

                                   

                                    for(int i = 1; i <= wy; i++)

                                    {

                                                DDA_ThinLine(x1, y1 - i, x2, y2 - i);

                                                DDA_ThinLine(x1, y1 + i, x2, y2 + i);

                                    }

                        }

                       

                        else

                        {          

wx = (w-1)*sqrt(pow((x2-x1),2)+pow((y2-y1),2))/(2*fabs(y2-y1)); 

                                    for(int i = 1; i <= wx; i++)

                                    {

                                                DDA_ThinLine(x1 - i, y1, x2 - i, y2); 

                                                DDA_ThinLine(x1 + i, y1, x2 + i, y2);

                                    }

                        }

           

            }

           

           

            // To draw a dashed line

            void DDA_DashedLine(int x1, int y1, int x2, int y2){

           

                        dx = x2 - x1;

                        dy = y2 - y1;

           

                        if(abs(dx) > abs(dy)){

                                    steps = abs(dx);

                        }

                        else{

                                    steps = abs(dy);

                        }

           

                        // Incremental values

                        Xinc = float(dx) / steps;

                        Yinc = float(dy) / steps;

           

                        Xi = x1;

                        Yi = y1;

                        putpixel(Xi, Yi, WHITE);          // starting points

           

                        for(int i = 1; i <= steps; i++)

                        {

                                    Xi = Xi + Xinc;

                                    Yi = Yi + Yinc;

                                   

                                    if(i % 4 == 0){

                                                Xi += (4*Xinc);

                                                Yi += (4*Yinc);

                                                i += 4;

                                    }

                                    else{

                                                delay(10);

                                                putpixel(round(Xi), round(Yi), WHITE);         

                                    }          

                        }

            }

           

           

            // To draw dotted line

            void DDA_DottedLine(int x1, int y1, int x2, int y2){

                       

                        dx = x2 - x1;

                        dy = y2 - y1;

           

                        if(abs(dx) > abs(dy)){

                                    steps = abs(dx);

                        }

                        else{

                                    steps = abs(dy);

                        }

           

                        // Incremental values

                        Xinc = float(dx) / steps;

                        Yinc = float(dy) / steps;

           

                        Xi = x1;

                        Yi = y1;

                        putpixel(Xi, Yi, WHITE);          // starting points

           

                        for(int i = 1; i <= steps; i++)

                        {

                                    Xi = Xi + Xinc;

                                    Yi = Yi + Yinc;

                                   

                                    if(i % 2 == 0){

                                                delay(5);

                                                putpixel(round(Xi), round(Yi), WHITE);

                                    }                      

                        }

            }          

};

 

int main()

{

            //         Starting and ending points

            int x1, x2, y1, y2;

            int ch;

            int w = 0;

           

            int gd = DETECT, gm;

            initgraph(&gd, &gm, "");

           

            cout<<"Enter starting points (x1, y1) : ";

            cin>>x1>>y1;

            cout<<"Enter ending points (x2, y2)   : ";

            cin>>x2>>y2;

           

            DDA obj;        

           

 

            do{

                        cout<<endl<<"Choose Operation :"<<endl

                        <<"1. Thin line"<<endl

                        <<"2. Thik line"<<endl

                        <<"3. Dashed line"<<endl

                        <<"4. Dotted line"<<endl

                        <<"Enter your choice : ";

                        cin>>ch;

                       

                        cleardevice();

                        switch(ch){

                                    case 1:obj.DDA_ThinLine(x1, y1, x2, y2);

                                    break;

                                    case 2:

                                                            cout<<endl<<"Enter the thikness : ";

                                                            cin>>w;

                                                            obj.DDA_ThikLine(x1, y1, x2, y2, w);

                                    break;

                                    case 3:obj.DDA_DashedLine(x1, y1, x2, y2);

                                    break;

                                    case 4:obj.DDA_DottedLine(x1, y1, x2, y2);

                                    break;

                                    case 5:exit(0);

                                    break;

                                    default:cout<<endl<<"Invalid choice..."<<endl;

                        }          

            }while(ch != 5);

           

           

           

            getch();

            closegraph();

           

            return 0;

}

 

·   Output

 

o   Thin Line

                    

o   Thick Line

Thickness  =  3 (User  Input)

 

                    

 

o   Dotted Line 

                    

 

o   Dashed Line 

                    

 

 

2)     To display the alphabets using DDA algorithm ( A  E F H I  K L M N T V W X Y Z).

And

3)     Write a program to display hut using DDA algorithm.

à

·    Source Code

 

#include<iostream>
using namespace std;
#include<conio.h>
#include<graphics.h>
#include<math.h>
 
class DDA{
           
            int dx, dy;                   
            float m;
            float Xi, Yi, Xinc, Yinc;
            int steps;
           
            public:
           
            void DDA_Line(int x1, int y1, int x2, int y2)
            {
                        dx = x2 - x1;
                        dy = y2 - y1;
           
                        if(abs(dx) > abs(dy)){
                                    steps = abs(dx);
                        }
                        else{
                                    steps = abs(dy);
                        }
           
                        // Incremental values
                        Xinc = float(dx) / steps;
                        Yinc = float(dy) / steps;
           
                        Xi = x1;
                        Yi = y1;
                        putpixel(Xi, Yi, WHITE);          // starting points
           
                        for(int i = 1; i <= steps; i++)
                        {
                                    Xi = Xi + Xinc;
                                    Yi = Yi + Yinc;
                                    delay(5);
                                    putpixel(round(Xi), round(Yi), WHITE);
                                    cout<<"Points plotted : ("<<Xi<<","<<Yi<<") Steps : "<<i<<endl;
                        }
                                   
            }
           
            void Alphabates()
            {
                        // To print 'a'
                        DDA_Line(100,50,50,150);
                        DDA_Line(100,50,150,150);
                        DDA_Line(75,100,125,100);
           
                        //         To print 'E'
                        DDA_Line(200,50,200,150);
                        DDA_Line(200,50,275,50);
                        DDA_Line(200,100,275,100);
                        DDA_Line(200,150,275,150);
           
                        //         To print 'F'
                        DDA_Line(300,50,300,150);
                        DDA_Line(300,50,375,50);
                        DDA_Line(300,100,375,100);
           
                        //         To print 'H'
                        DDA_Line(400,50,400,150);
                        DDA_Line(400,100,475,100);
                        DDA_Line(475,50,475,150);
           
                        //         To print 'I'
                        DDA_Line(500,50,575,50);
                        DDA_Line(537,50,537,150);
                        DDA_Line(500,150,575,150);
                                   
                        //         To print 'K'
                        DDA_Line(600,50,600,150);
                        DDA_Line(600,100,675,50);
                        DDA_Line(600,100,675,150);
           
                        //         To print 'L'
                        DDA_Line(700,50,700,150);
                        DDA_Line(700,150,775,150);
           
                        //         To print 'M'
                        DDA_Line(800,50,800,150);
                        DDA_Line(800,50,837,100);
                        DDA_Line(837,100,875,50);
                        DDA_Line(875,50,875,150);
           
                        //         To print 'N'
                        DDA_Line(900,50,900,150);
                        DDA_Line(900,50,975,150);
                        DDA_Line(975,150,975,50);
           
                        //         To print 'T'
                        DDA_Line(1000,50,1075,50);
                        DDA_Line(1037,50,1037,150);
 
                        //         To print 'V'
                        DDA_Line(1100,50,1137,150);
                        DDA_Line(1137,150,1175,50);
 
                       
//         To print 'W'
                        DDA_Line(1200,50,1200,150);
                        DDA_Line(1200,150,1237,100);
                        DDA_Line(1237,100,1275,150);
                        DDA_Line(1275,150,1275,50);
 
                        //         To print 'X'
                        DDA_Line(50,250,125,350);
                        DDA_Line(50,350,125,250);
 
                        //         To print 'Y'
                        DDA_Line(150,250,187,300);
                        DDA_Line(187,300,225,250);
                        DDA_Line(187,300,187,350);
 
                        //         To print 'Z'
                        DDA_Line(250,250,325,250);
                        DDA_Line(325,250,250,350);
                        DDA_Line(250,350,325,350);
            }
           
            void Hut()
            {
                        DDA_Line(400, 350, 400, 600);
                        DDA_Line(400, 600, 1000, 600);
                        DDA_Line(1000, 600, 1000, 350);
                        DDA_Line(1000, 350, 400, 350);
                       
                        DDA_Line(400, 350, 500, 200);
                        DDA_Line(500, 200, 600, 350);
                        DDA_Line(500, 200, 900, 200);
                        DDA_Line(900, 200, 1000, 350);
                       
                        DDA_Line(600, 350, 600, 600);
                       
                        DDA_Line(460, 600, 460, 470);
                        DDA_Line(460, 470, 540, 470);
                        DDA_Line(540, 470, 540, 600);
            }
};
 
int main()
{
           
            int gd = DETECT, gm;
            initgraph(&gd, &gm, "");
           
            DDA obj;        
           
            int ch;
            do
            {
                        cout<<endl<<"1. Print all Alphabates : ";
                        cout<<endl<<"2. Print the Hut : ";
                        cout<<endl<<"3. Exit : ";
                        cout<<endl<<"Enter Your Choice : ";
                        cin>>ch;
                       
                        cleardevice();
                       
                        switch(ch){
                                    case 1:
                                                obj.Alphabates();
                                                break;
                                   
                                    case 2:
                                                obj.Hut();
                                                break;
                                               
                                    case 3:
                                                exit(0);
                                   
                                    default:
                                                cout<<endl<<"Invalid choice"<<endl;
                        }
                       
            }while(ch != 3);
           
           
            getch();
            closegraph();
           
            return 0;
}
 
 

·       Output

o   Alphabets

                     

                    

o   Hut

                   


 

4)     To  draw a Thin line by using DDA for slops is less than one and greater than one for different cases. 

à

·           Source Code

 

#include<iostream>
using namespace std;
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<string.h>
 
 
class DDA_slopeCases{
            int dx, dy;                   
            float m;

            float Xi, Yi;
            float Xinc, Yinc;

            int steps;
            string text = "Slope : ";

            int c;
           

            public:
                        void drawGraph();

                        void slopeLessThan_one(int x1, int y1, int x2, int y2,int color);

                        void slopeGreaterThan_one(int x1, int y1, int x2, int y2, int color);

   };

 

            void DDA_slopeCases :: drawGraph()
            {

                        line(400, 400, 1200, 400);
                        line(800, 50, 800, 750);

            }
 

void DDA_slopeCases :: slopeLessThan_one(int x1, int y1, int x2, int y2, int color)

            {
                        c = 0;

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

                        cout<<endl<<"dx : "<<dx<<" dy : "<<dy<<endl;
           

                        // To find slope(m)
                        m = (float)dy / (float)dx;

                        cout<<endl<<"Slope is "<<m<<endl;
           

           
                        // To calculate steps
                        if(abs(dx) >= abs(dy))

                        {
                                    steps = abs(dx);

                        }
                        else

                        {
                                    steps = abs(dy);

                        }
           

           
                        // To check cases
                        if(dx > 0 && dy > 0)

                        {
                                    Xinc = 1;

                                    Yinc = fabs(m);
                                    c = 1;

                        }
                        else if(dx > 0 && dy < 0)

                        {
                                    cout<<endl<<"here..."<<endl;

                                    Xinc = 1;
                                    Yinc = -(fabs(m));

                                    cout<<"\nYinc : "<<Yinc<<endl;
                                    c = 2;

                        }
                        else if(dx < 0 && dy > 0)

                        {
                                    Xinc = -1;

                                    Yinc = fabs(m);
                                    c = 3;

                        }
                        else if(dx < 0 && dy < 0)

                        {
                                    Xinc = -1;

                                    Yinc = -(fabs(m));
                                    c = 4;

                        }
                       

                        Xi = x1;
                        Yi = y1;

                        putpixel(Xi, Yi, color);
           

                        for(int i = 1; i <= steps; i++)
                        {

                       
                                    Xi = (Xi + Xinc);
                                    Yi = (Yi + Yinc);

                                    delay(1);
                                    putpixel(round(Xi), round(Yi), color);

                                    cout<<"Points plotted : ("<<Xi<<","<<Yi<<") Steps : "<<i<<endl;
                       

                        }
                       

                        if(c == 1){
                                    outtextxy(Xi, Yi, "(dx > 0 & dy > 0 | m < 1)");

                        }
                        else if(c == 2){

                                    outtextxy(Xi, Yi, "(dx > 0 & dy < 0 | m < 1)");
                        }

                        else if(c == 3){
                                    outtextxy(Xi, Yi, "(dx < 0 & dy > 0 | m < 1)");

                        }
                        else if(c == 4){

                                    outtextxy(Xi, Yi, "(dx < 0 & dy < 0 | m < 1)");
                        }

 
            }
 

 

void DDA_slopeCases :: slopeGreaterThan_one(int x1, int y1, int x2, int y2, int color)

            {
                        c = 0;

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

                        cout<<endl<<"dx : "<<dx<<" dy : "<<dy<<endl;
           

                        // To find slope(m)
                        m = (float)dy / (float)dx;

                        cout<<endl<<"Slope is "<<m<<endl;
           

           
                        // To calculate steps
                        if(abs(dx) >= abs(dy))

                        {
                                    steps = abs(dx);

                        }
                        else

                        {
                                    steps = abs(dy);

                        }
           

           
                        // To check cases
                       

                        if(dx > 0 && dy > 0)
                        {

                                    Xinc = fabs(1/m);
                                    Yinc = 1;

                                    c = 1;
                        }

                        else if(dx > 0 && dy < 0)
                        {

                                    Xinc = fabs(1/m);
                                    Yinc = -1;

                                    c = 2;
                        }

                        else if(dx < 0 && dy > 0)
                        {

                                    Xinc = -(fabs(1/m));
                                    Yinc = 1;

                                    c = 3;
                        }

                        else if(dx < 0 && dy < 0)
                        {

                                    Xinc = -(fabs(1/m));
                                    Yinc = -1;

                                    c = 4;
                        }

                       
                        Xi = x1;
                        Yi = y1;

                        putpixel(Xi, Yi, color);
           

                        for(int i = 1; i <= steps; i++){
                       

                                    Xi = (Xi + Xinc);
                                    Yi = (Yi + Yinc);

                                    delay(10);
                                    putpixel(round(Xi), round(Yi), color);

                                    cout<<"Points plotted : ("<<Xi<<","<<Yi<<")"<<endl;
                                   

                        }
                       

                       
                        if(c == 1){
                                    outtextxy(Xi, Yi, "(dx > 0 & dy > 0 | m > 1)");

                        }
                        else if(c == 2){

                                    outtextxy(Xi, Yi, "(dx > 0 & dy < 0 | m > 1)");
                        }

                        else if(c == 3){
                                    outtextxy(Xi, Yi, "(dx < 0 & dy > 0 | m > 1)");

                        }
                        else if(c == 4){

                                    outtextxy(Xi, Yi, "(dx < 0 & dy < 0 | m > 1)");
                        }


           
            }

int main()
{
            int gd = DETECT, gm;
            initgraph(&gd, &gm, "");
           
            DDA_slopeCases obj;
           
            int ch;
            do{
                        cout<<endl<<"1. Slope less than 1"<<endl
                                                <<"2. Slope greater than 1"<<endl
                                                <<"3. Exit"<<endl;
                        cout<<"Enter your choice : ";
                        cin>>ch;
                       
                        cleardevice();
                       
                        switch(ch)
{
                        case 1:
                            obj.drawGraph();
                            obj.slopeLessThan_one(800,400,1150,250,4);   //(dx>0 & dy<0)
                            obj.slopeLessThan_one(800,400,450,550,1);     //(dx<0 & dy>0)
                            obj.slopeLessThan_one(800,400,1150,550,2);   //(dx>0 & dy>0)
                            obj.slopeLessThan_one(800,400,450,250,14);   //(dx<0 & dy<0)
                            break;
                                   
                        case 2:
                            obj.drawGraph();
                            obj.slopeGreaterThan_one(800,400,950,100,4);   //(dx>0 & dy<0)
                            obj.slopeGreaterThan_one(800,400,650,700,1);   //(dx<0 & dy>0)
                            obj.slopeGreaterThan_one(800,400,950,700,2);   //(dx>0 & dy>0)
                            obj.slopeGreaterThan_one(800,400,650,100,14); //(dx<0 & dy<0)
                            break;
                                   
                        case 3:
    exit(0);
                            break;
                                   
                        default:
                            cout<<endl<<"Invalid choice..."<<endl;
                       
}
                     
            }while(ch!=3);
           
            getch();
            closegraph();
           
            return 0;

}

 

·       Output

o   Slope less than 1

 

                

o   Slope greater than 1







 

o   Advantages :

 

1)     It is simple and easy to implement algorithm.

2)     It avoid using multiple operations which have high time complexities.

3)     It is faster than the direct use of the line equation because it does not use any floating point multiplication and it calculates points on the line.

 

o   Disadvantages :

 

1)     It deals with the rounding off operation and floating point arithmetic so it has high time complexity.

2)     As it is orientation dependent, so it has poor endpoint accuracy.

3)     Due to the limited precision in the floating point representation it produces cumulative error.






Comments